-1

Hi I'm new to xml files in general, but I am trying to replace specific lines in a xml file using 'if statements' in python 3.6. I've been looking at suggestions to use ElementTree, but none of the posts online quite fit the problem I have, so here I am.

My file is as followed:

<?xml version="1.0" encoding="UTF-8"?>

-<StructureDefinition xmlns="http://hl7.org/fhir">

    <url value="http://example.org/fhir/StructureDefinition/MyObservation"/>

    <name value="MyObservation"/>

    <status value="draft"/>

    <fhirVersion value="3.0.1"/>

    <kind value="resource"/>

    <abstract value="false"/>

    <type value="Observation"/>

    <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Observation"/>

    <derivation value="constraint"/>

</StructureDefinition>

I want to replace

url value="http://example.org/fhir/StructureDefinition/MyObservation"/

to something like

url value="http://example.org/fhir/StructureDefinition/NewObservation"/

by using conditional statements - because these are repeated multiple times in other files.

I have tried for-looping through the xml find to find the exact string match (which I've succeeded), but I wasn't able to delete, or replace the line (probably having to do with the fact that this isn't a .txt file). Any help is greatly appreciated!

June
  • 33
  • 5

1 Answers1

0

Your sample file contains a "-"-token in ln 3 that may be overlooked when copy/pasting in order to find a solution.

Input File

<?xml version="1.0" encoding="UTF-8"?>

<StructureDefinition xmlns="http://hl7.org/fhir">

    <url value="http://example.org/fhir/StructureDefinition/MyObservation"/>

    <name value="MyObservation"/>

    <status value="draft"/>

    <fhirVersion value="3.0.1"/>

    <kind value="resource"/>

    <abstract value="false"/>

    <type value="Observation"/>

    <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Observation"/>

    <derivation value="constraint"/>

</StructureDefinition>

Script

from xml.dom.minidom import parse # use minidom for this task
dom = parse('june.xml') #read in your file
search = "http://example.org/fhir/StructureDefinition/MyObservation" #set search value
replace = "http://example.org/fhir/StructureDefinition/NewObservation" #set replace value
res = dom.getElementsByTagName('url') #iterate over url tags
for element in res:
    if element.getAttribute('value') == search: #in case of match
        element.setAttribute('value', replace) #replace
with open('june_updated.xml', 'w') as f:
    f.write(dom.toxml()) #update the dom, save as new xml file

Output file

<?xml version="1.0" ?><StructureDefinition xmlns="http://hl7.org/fhir">

    <url value="http://example.org/fhir/StructureDefinition/NewObservation"/>

    <name value="MyObservation"/>

    <status value="draft"/>

    <fhirVersion value="3.0.1"/>

    <kind value="resource"/>

    <abstract value="false"/>

    <type value="Observation"/>

    <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Observation"/>

    <derivation value="constraint"/>

</StructureDefinition>
The Weckness
  • 111
  • 4
  • Thank you so much! that was very quick and easy, and easy syntax to understand. I just have one problem, which is that after running the above code, some of the text in my file became corrupted (I didn't upload the full file because it was too long). I had some Korean language in the original file, which became gibberish afterwards. Any hints on how to fix this? – June Mar 29 '19 at 03:25
  • You can specify the coding (as indicated in your original file) to be UTF-8 again when writing the file like so: `with open('june_updated.xml', 'w', encoding = 'utf-8')`. I tried by adding a random 한글 that did not appear funky after running the script – The Weckness Mar 29 '19 at 03:51
  • Awesome, the Korean is in intact, thanks for the help once again! – June Mar 29 '19 at 03:54