how to replace elements in xml using python -


sorry poor english. need ;(

i have 2 xml files.

one is:

<root> <data name="aaaa"> <value>"old value1"</value> <comment>"this old value1 of aaaa"</comment> </data> <data name="bbbb">  <value>"old value2"</value> <comment>"this old value2 of bbbb"</comment> </data> </root> 

two is:

<root> <data name="aaaa"> <value>"value1"</value> <comment>"this value 1 of aaaa"</comment> </data> <data name="bbbb">  <value>"value2"</value> <comment>"this value2 of bbbb"</comment> </data> <data name="cccc">  <value>"value3"</value> <comment>"this value3 of cccc"</comment> </data> </root> 

one.xml updated two.xml.

so, one.xml should this.

one.xml(after) :

<root> <data name="aaaa"> <value>"value1"</value> <comment>"this value1 of aaaa"</comment> </data> <data name="bbbb">  <value>"value2"</value> <comment>"this value2 of bbbb"</comment> </data> </root> 

data name="cccc" not exist in one.xml. therefore ignored.

actually want

  1. download two.xml(whole list) db
  2. update one.xml (it contains data-lists app uses) two.xml

any can me please !! thanks!!

============================================================== xml.etree.elementtree
code works example. found problem in real xml file.

the real one.xml contains :

<?xml version="1.0" encoding="utf-8"?> <root>     <resheader name="resmimetype">         <value>text/microsoft-resx</value>     </resheader>     <resheader name="version">         <value>2.0</value>     </resheader>     <resheader name="reader">         <value>system.resources.resxresourcereader, system.windows.forms, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</value>     </resheader>     <resheader name="writer">         <value>system.resources.resxresourcewriter, system.windows.forms, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</value>     </resheader>      <data name="noteslabel" xml:space="preserve">         <value>hinweise:</value>         <comment>label input field</comment>     </data>     <data name="notesplaceholder" xml:space="preserve">         <value>z . milch kaufen</value>         <comment>example input notes field</comment>     </data>     <data name="addbutton" xml:space="preserve">         <value>neues element hinzufügen</value>         <comment>this string appears on button add new item list</comment>     </data> </root> 

it seems, resheader causes trouble. have idea fix?

you can use xml.etree.elementtree , while there propably more elegant ways, should work on files fit in memory if names unique in two.xml

import xml.etree.elementtree et tree_one = et.parse('one.xml') root_one = tree_one.getroot() tree_two = et.parse('two.xml') root_two = tree_two.getroot()  data_two=dict((e.get("name"), e) e in root_two.findall("data"))  eo in root_one.findall("data"):     name=eo.get("name")     tail=eo.tail     eo.clear()     eo.tail=tail     en=data_two[name]     k,v in en.items():         eo.set(k,v)     eo.extend(en.findall("*"))     eo.text=en.text  tree_one.write("one.xml") 

if files not fit in memory can still use xml.dom.pulldom long single data entries fit.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -