Compare two XML files and update elements in one of them

ghostriker

New Member
I am sorry if my title sounds confusing.I am writing a Python script that compares 2 XML files. In both files we have data for which the id's are equal to those in the other file.E.g.Source file: \[code\] <id>123456</id> <data>blabla</data> ......some other data...... <id>abcde</id> <data>gfkgjk</data> ......some more data..........\[/code\]Target file:\[code\] <id>123456</id> <data> </data> ......some other data...... <id>ghijk</id> <data>gfkgjk</data> ......some more data..........\[/code\]As you can see in the above examples, not all ID's that are in the source file are also in the target file. Furthermore, although 2 data groups have the same ID, one has the "data" tags filled out, the other hasn't.My programme is supposed to have a look at the source file, extract the id and the text between the data tags. Then it looks into the target file, and if there is data with the same ID and empty data tags (like in the example above), it fills in these empty tags with the information from the source file. (By the way: apart from the ID and the data information, the two XMLs are completely different, therefore I can't just keep the source file).Right, I was able to extract the ID and the info between the data tags.Now I'm trying to write a function to compare the ids and replace the empty data info if there is one. However, I'm not very familiar with Python and functions and need some help.Here is what my function looks like:\[code\]def replace_empty_data(): for x in xmlData_id_source: if xmlData_id_source==xmlData_id_target: target = re.sub(xmlData_2,xmlData,target) return target file_target.close()\[/code\]There's probably loads missing in the function, but I don't know what. It doesn't give me any errors and is simply not working. Variables except x have been defined in earlier parts of the code, so this is not an issue.xmlData_id_source is the ID from the source filexmlData_id_target is the ID from the target filexmlData_2 is the data information from the target filexmlData is the data information from the source fileThanks for the input so far, but I'm still looking for an easy-to-understand method for someone who has no clue about programming....I used minidom for parsing the files and would like to use it without importing and installing further libraries.
 
Back
Top