Python Etree replace content in node
===

I'd like to parse and compare 2 XML files with the Python Etree parser as follows:

I have 2 XML files with loads of data. One is in English (the source file), the other one the corresponding French translation (the target file).
E.g.:
source file:

Code:

    <macro>
       <id> 123</id>
              <string> DOG </string>
              <string>dogs/dog/dog's</string>
              <string>Cross-language reference</string>
              <string>English dog: dogs/dog/dog's</string>
      (..........)
    <macro>



Code:

target file:

    <macro>
       <id> 123</id>
              <string> CHIEN </string>
              <string>chien/chiens</string>
              <string>Cross-language reference</string>
              <string></string>
      (..........)
    <macro>


The French target file has an empty cross-language reference where I'd like to put in the information from the English source file whenever the 2 macros have the same ID.
I already wrote some code in which I replaced the string tag name with a unique tag name in order to identify the cross-language reference. Now I want to compare the 2 files and if 2 macros have the same ID, exchange the empty reference in the French file with the info from the English file. I was trying out the minidom parser before but got stuck and would like to try Etree now. I have hardly any knowledge about programming and find this very hard.
Here is the code I have so far:


        import xml.etree.ElementTree as ElementTree
        import re
        #open the xml file for reading that contains the correct CL references:
        file = open('PATH/english.xml','r+')
        #open the xml file for reading that contains the missing CL references:
        file_target =  open('PATH/french.xml','r+')
        #convert to string:
        data = file.read()
        #replace xml tag with a unique name in order to identify it later on
        data = re.sub(r"<string>Cross-language reference</string>(\s+)<string>(.*)</string>",r"<cl>Cross-language reference</cl>\1<cl>\2</cl>",data)
        file.seek(0)
        file.write(data)
        #remove old data
        file.truncate()
        #close file because we dont need it anymore:
        file.close()
        #convert to string:
        target = file_target.read()
        #replace xml tag with a unique name in order to identify it later on
        target = re.sub(r"<string>Cross-language reference</string>(\s+)<string>(.*)</string>",r"<cl>Cross-language reference</cl>\1<cl>\2</cl>",target)
        file_target.seek(0)
        file_target.write(target)
        #remove old data
        file_target.truncate()
        #close file because we dont need it anymore:
        file_target.close()

        macros = ElementTree.parse(data)

        for tag in macros.getchildren('macro'):
            id_ = tag.find('id')
            data = tag.find('cl')
            id_dict[id_.text] = data.text

        macros = ElementTree.parse(target)

        for tag in macros.getchildren('macro'):
            id_ = tag.find('id')
            target = tag.find('cl')
            if target.text.strip() == '':
            target.text = id_dict[id_.text]

        print (ElementTree.tostring(macros))

I am more than clueless and reading other posts on this confuses me even more. I'd appreciate it very much if someone could enlighten me :-)

