英文:
How to iterate over XML children with same name as current element and avoid current element in iteration?
问题
我有
一个给定的XML(不能更改命名),其中一个节点和其直接子节点具有相同的名称,这里是 items
我想
只迭代子节点,具有 description
字段的 items
我的问题
类型为 items
的父节点出现在迭代中,即使我理解正确,iter
在自身上调用
from xml.etree import ElementTree
content = """<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<items>
<description>foo1</description>
</items>
<items>
<description>foo2</description>
</items>
</items>
</root>
"""
tree = ElementTree.fromstring(content)
print(">>", tree.find("items",))
for item in tree.find("items").iter("items"):
print(item, item.find("description"))
当前输出
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8720> None
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
预期输出
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
英文:
I have
A given XML (can't change naming) that have same name for a node and its direct children, here items
I want
To iterate on the children only, the items
that have a description
field
My issue
The parent node of type items
appears in the iteration, even the iter
is called on itself if I understand well
from xml.etree import ElementTree
content = """<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<items>
<description>foo1</description>
</items>
<items>
<description>foo2</description>
</items>
</items>
</root>
"""
tree = ElementTree.fromstring(content)
print(">>", tree.find("items"))
for item in tree.find("items").iter("items"):
print(item, item.find("description"))
Current output
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8720> None
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
Expected output
>> <Element 'items' at 0x0000020B5CBF8720>
<Element 'items' at 0x0000020B5CBF8770> <Element 'description' at 0x0000020B5CBF87C0>
<Element 'items' at 0x0000020B5CBF8810> <Element 'description' at 0x0000020B5CBF8860>
答案1
得分: 1
tree.findall('items/items')
英文:
Use XPath with findall().
tree.findall('items/items')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论