简体   繁体   English

如何使用python替换xml中的元素

[英]how to replace elements in xml using python

sorry for my poor English. 抱歉我的英语不好。 but i need your help ;( 但我需要你的帮助;(

i have 2 xml files. 我有2个xml文件。

one is: 一个是:

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

two is: 二是:

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

one.xml will be updated from two.xml. one.xml将从two.xml更新。

so, the one.xml should be like this. 所以,one.xml应该是这样的。

one.xml(after) : one.xml(后):

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

data name="cccc" is not exist in one.xml. one.xml中不存在data name =“cccc”。 therefore ignored. 因此忽略了

actually what i want to do is 实际上我想做的是

  1. download two.xml(whole list) from db 从db下载two.xml(整个列表)
  2. update my one.xml (it contains DATA-lists that only the app uses) by two.xml 通过two.xml更新我的one.xml(它包含只有应用程序使用的DATA列表)

Any can help me please !! 任何可以帮助我! Thanks!! 谢谢!!

============================================================== xml.etree.ElementTree ================================================== ============ xml.etree.ElementTree
your code works with the example. 您的代码适用于示例。 but i found a problem in real xml file. 但我在真正的xml文件中发现了一个问题。

the real one.xml contains : 真正的one.xml包含:

<?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 for input field</comment>
    </data>
    <data name="NotesPlaceholder" xml:space="preserve">
        <value>z . Milch kaufen</value>
        <comment>example input for notes field</comment>
    </data>
    <data name="AddButton" xml:space="preserve">
        <value>Neues Element hinzufügen</value>
        <comment>this string appears on a button to add a new item to the list</comment>
    </data>
</root>

it seems, resheader causes trouble. 看起来,重新导致麻烦。 do you have any idea to fix? 你有任何想法修复?

You can use xml.etree.ElementTree and while there are propably more elegant ways, this should work on files that fit in memory if name s are unique in two.xml 您可以使用xml.etree.ElementTree ,虽然有更优雅的方式,但如果nametwo.xml是唯一的, two.xml这应适用于适合内存的文件

import xml.etree.ElementTree as 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) for e in root_two.findall("data"))

for eo in root_one.findall("data"):
    name=eo.get("name")
    tail=eo.tail
    eo.clear()
    eo.tail=tail
    en=data_two[name]
    for k,v in en.items():
        eo.set(k,v)
    eo.extend(en.findall("*"))
    eo.text=en.text

tree_one.write("one.xml")

If your files do not fit in memory you can still use xml.dom.pulldom as long as single data entries do fit. 如果您的文件不适合内存,只要单个data条目适合,您仍然可以使用xml.dom.pulldom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM