Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从XML中删除父元素和所有子元素_Python_Xml_Python 2.7_Elementtree - Fatal编程技术网

Python 从XML中删除父元素和所有子元素

Python 从XML中删除父元素和所有子元素,python,xml,python-2.7,elementtree,Python,Xml,Python 2.7,Elementtree,给定具有以下结构的XML文件: <Root> <Stuff></Stuff> <MoreStuff></MoreStuff> <Targets> <Target> <ID>12345</ID> <Type>Ground</Type> <Size>

给定具有以下结构的XML文件:

<Root>
    <Stuff></Stuff>
    <MoreStuff></MoreStuff>
    <Targets>
        <Target>
            <ID>12345</ID>
            <Type>Ground</Type>
            <Size>Large</Size>
        </Target>
        <Target>
            ...
        </Target>
    </Targets>
</Root>

这种方法的问题是只删除了
子元素,但是我需要删除整个
元素及其所有子元素。有人能帮忙吗!谢谢。

不幸的是,元素树元素不知道他们的父母是谁。有一个解决办法--


未测试

您需要保留对Targets元素的引用,以便可以删除其子元素,因此从那里开始迭代。抓住每一个目标,检查你的状况,移除你不喜欢的东西

#!/usr/bin/env python
import xml.etree.ElementTree as ET

xmlstr="""<Root>
    <Stuff></Stuff>
    <MoreStuff></MoreStuff>
    <Targets>
        <Target>
            <ID>12345</ID>
            <Type>Ground</Type>
            <Size>Large</Size>
        </Target>
        <Target>
            ...
        </Target>
    </Targets>
</Root>"""

root = ET.fromstring(xmlstr)

targets = root.find('Targets')

for target in targets.findall('Target'):
    _id = target.find('ID')
    if _id is not None and '12345' in _id.text:
        targets.remove(target)

print ET.tostring(root)
#/usr/bin/env python
将xml.etree.ElementTree作为ET导入
xmlstr=”“”
12345
地面
大的
...
"""
root=ET.fromstring(xmlstr)
targets=root.find('targets'))
对于targets.findall('target')中的目标:
_id=target.find('id'))
如果_id不是None并且在_id.text中为“12345”:
目标。删除(目标)
打印ET.tostring(根目录)

+1即使它未经测试,因为它可能有效,或者至少是解决方案的99%。
tree = ET.parse('file.xml')
root = tree.getroot()
parent_map = dict((c, p) for p in tree.getiterator() for c in p)

# list so that we don't mess up the order of iteration when removing items.
iterator = list(root.getiterator('Target'))

for item in iterator:
    old = item.find('ID')
    text = old.text
    if '12345' in text:
        parent_map[item].remove(item)
        continue

tree.write('out.xml')
#!/usr/bin/env python
import xml.etree.ElementTree as ET

xmlstr="""<Root>
    <Stuff></Stuff>
    <MoreStuff></MoreStuff>
    <Targets>
        <Target>
            <ID>12345</ID>
            <Type>Ground</Type>
            <Size>Large</Size>
        </Target>
        <Target>
            ...
        </Target>
    </Targets>
</Root>"""

root = ET.fromstring(xmlstr)

targets = root.find('Targets')

for target in targets.findall('Target'):
    _id = target.find('ID')
    if _id is not None and '12345' in _id.text:
        targets.remove(target)

print ET.tostring(root)