如何使用 python 获取 XML 的所有子节点?

新手上路,请多包涵

这是我的 XML 文件

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdp>141100</gdp>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
</data>

如何拉取 country 的所有子节点?

例如,我需要输出为 ['rank','year','gdp','neighbor']

原文由 radh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.8k
2 个回答

使用 ElementTree 库拉出子节点。这可能对你有帮助。

 import xml.etree.ElementTree as ET
tree = ET.parse("file.xml")
root = tree.getroot()
for child in root:
  print({x.tag for x in root.findall(child.tag+"/*")})

原文由 voidpro 发布,翻译遵循 CC BY-SA 3.0 许可协议

查看 python 文档。它确实以这个 xml 树为例。

 import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

country = root[0].getchildren()
map(lambda e: e.tag, r)
# ['rank', 'year', 'gdp', 'neighbor', 'neighbor']

btw,卡的时候打开repl,一步一步来。我不记得上面所有这些东西。最后一次使用 xml 解析器是在 2 或 3 年前。但我知道,“试试看”是最好的老师。

这些是步骤,我如何提出解决方案。

 # imports and other stuff.
>>> tree = ET.parse('data.xml')
>>> root = tree.getroot()
>>> country = root[0]
>>> dir(country)
['__class__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_children', 'append', 'attrib', 'clear', 'copy', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'iter', 'iterfind', 'itertext', 'keys', 'makeelement', 'remove', 'set', 'tag', 'tail', 'text']
>>> country.keys()
['name']
>>> country.getchildren()
[<Element 'rank' at 0x7f873cf53910>, <Element 'year' at 0x7f873cf539d0>, <Element 'gdp' at 0x7f873cf53a90>, <Element 'neighbor' at 0x7f873cf53c10>, <Element 'neighbor' at 0x7f873cf53c50>]
>>> country.getchildren()[0]
<Element 'rank' at 0x7f873cf53910>
>>> r = country.getchildren()[0]
>>> dir(r)
['__class__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_children', 'append', 'attrib', 'clear', 'copy', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'iter', 'iterfind', 'itertext', 'keys', 'makeelement', 'remove', 'set', 'tag', 'tail', 'text']
>>> r.tag
'rank'
>>> r = country.getchildren()[0]
>>> r
<Element 'rank' at 0x7f873cf53910>
>>> r = country.getchildren()
>>> r
[<Element 'rank' at 0x7f873cf53910>, <Element 'year' at 0x7f873cf539d0>, <Element 'gdp' at 0x7f873cf53a90>, <Element 'neighbor' at 0x7f873cf53c10>, <Element 'neighbor' at 0x7f873cf53c50>]
>>> map(lambda e: e.tag, r)
['rank', 'year', 'gdp', 'neighbor', 'neighbor']

原文由 marmeladze 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题