使用python删除XML中的子元素 [英] Removing child elements in XML using python
问题描述
Python 3.2.5 x64
ElementTree
Python 3.2.5 x64 ElementTree
我有一些需要使用python格式化的数据。
本质上我有包含元素和子元素的文件。我需要删除其中一些元素的子元素。我已经检查了之前的问题,但无法解决。到目前为止,我得到的最好的结果只是删除了第二个子元素。
I have data that I need to format using python. Essentially I have file with elements and subelements. I need to delete the child elements of some of these elements. I have checked previous questions and I couldn't make a solution. The best I had so far only removes every second child element.
样本数据:
<Leg1:MOR oCount="7" xmlns:Leg1="http://what.not">
<Leg1:Order>
<Leg1:CTemp id="FO">
<Leg1:Group bNum="001" cCount="4">
<Leg1:Dog ndate="112" pdate="111"/>
<Leg1:Dog ndate="122" pdate="121"/>
<Leg1:Dog ndate="132" pdate="131"/>
<Leg1:Dog ndate="142" pdate="141"/>
</Leg1:Group>
<Leg1:Group bNum="002" cCount="4">
<Leg1:Dog ndate="112" pdate="111"/>
<Leg1:Dog ndate="122" pdate="121"/>
<Leg1:Dog ndate="132" pdate="131"/>
<Leg1:Dog ndate="142" pdate="141"/>
</Leg1:Group>
</Leg1:CTemp>
<Leg1:CTemp id="GO">
<Leg1:Group bNum="001" cCount="4">
<Leg1:Dog ndate="112" pdate="111"/>
<Leg1:Dog ndate="122" pdate="121"/>
<Leg1:Dog ndate="132" pdate="131"/>
<Leg1:Dog ndate="142" pdate="141"/>
</Leg1:Group>
<Leg1:Group bNum="002" cCount="4">
<Leg1:Dog ndate="112" pdate="111"/>
<Leg1:Dog ndate="122" pdate="121"/>
<Leg1:Dog ndate="132" pdate="131"/>
<Leg1:Dog ndate="142" pdate="141"/>
</Leg1:Group>
</Leg1:CTemp>
</Leg1:Order>
</Leg1:MOR>
我需要的输出看起来像:
What I need the output to look like:
<Leg1:MOR oCount="7" xmlns:Leg1="http://what.not">
<Leg1:Order>
<Leg1:CTemp id="FO">
<Leg1:Group bNum="001" cCount="10"/>
<Leg1:Group bNum="002" cCount="10"/>
</Leg1:CTemp>
<Leg1:CTemp id="GO">
<Leg1:Group bNum="001" cCount="10"/>
<Leg1:Group bNum="002" cCount="10"/>
</Leg1:CTemp>
</Leg1:Order>
</Leg1:MOR>
我有一段时间没有写任何东西了,我的代码也没用。我可以解析文件,然后将其写入,但我无法正确处理。
I haven't written anything in a while and my code is useless. I can parse the file, and write it I cannot get the processing right.
import xml.etree.cElementTree as ET
tree = ET.parse("input.xml")
root = tree.getroot()
for x in root.findall('./Order/CTemp/Group'):
root.remove(x)
tree.write("output.xml")
如何删除 CTemp
元素的 Dog
子级?
推荐答案
如果可以使用 lxml ,请尝试以下操作:
If you can use lxml, try this:
import lxml.etree
tree = lxml.etree.parse("leg.xml")
for dog in tree.xpath("//Leg1:Dog",
namespaces={"Leg1": "http://what.not"}):
parent = dog.xpath("..")[0]
parent.remove(dog)
parent.text = None
tree.write("leg.out.xml")
现在 leg.out.xml
看起来像这样:
<?xml version="1.0"?>
<Leg1:MOR xmlns:Leg1="http://what.not" oCount="7">
<Leg1:Order>
<Leg1:CTemp id="FO">
<Leg1:Group bNum="001" cCount="4"/>
<Leg1:Group bNum="002" cCount="4"/>
</Leg1:CTemp>
<Leg1:CTemp id="GO">
<Leg1:Group bNum="001" cCount="4"/>
<Leg1:Group bNum="002" cCount="4"/>
</Leg1:CTemp>
</Leg1:Order>
</Leg1:MOR>
这篇关于使用python删除XML中的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!