BrokenLegMike

Find and Replace String Won't Save to New File

Discussion created by BrokenLegMike on Jul 20, 2011
Latest reply on Jul 20, 2011 by BrokenLegMike
Hey everyone,

I am trying to parse out a template XML file to find and replace embedded key words in order to output a new XML files with the replaced values. The key words are text values within the tags. I am not looking to replace tags, attributes or anything else. I am going to use these XML's to upload as metadata into some feature classes.

My issue is, when I write the new file, the replaced key words remain unchanged. I've played around with the indentation on the write line, but nothing seems to populate an edited XML.


root = ElementTree(file=r"Z:\ESRI\Figures\test.xml")

    #Create an iterator
    iter = root.getiterator()
    #Iterate
    for element in iter:

        if "%SITEDESCR%" in element.text:
            print element.text
            elementtext = element.text.replace("%SITEDESCR%", "Pigeon Lake")
            print elementtext
    root.write(r"Z:\ESRI\Figures\test_edit.xml")



My print lines work. The first one prints the original string in the tag and the second print line shows that the edited string.


>>>
This dataset represents the polygons developed from the field data for %SITEDESCR%.
This dataset represents the polygons developed from the field data for Pigeon Lake.
>>>


Any suggestions?

Thanks,
Mike

Outcomes