这是我的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']
答案 0 :(得分:3)
使用ElementTree lib拉出子节点。这可能会对你有所帮助。
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+"/*")})
答案 1 :(得分:2)
使用xml.etree.ElementTree模块的解决方案:
import xml.etree.ElementTree as ET
tree = ET.parse("yourxml.xml")
root = tree.getroot()
tag_names = {t.tag for t in root.findall('.//country/*')}
print(tag_names) # print a set of unique tag names
输出:
{'gdp', 'rank', 'neighbor', 'year'}
'.//country/*'
- xpath表达式,用于提取节点country
答案 2 :(得分:1)
Have a look up to python documentation。它确实使用这个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']
是的,当你被困住时,打开repl并一步一步地走。我不记得以上所有这些。最后在2年或3年前使用过xml解析器。但我知道,&#34;试着看看&#34;是最好的老师。
这些是步骤,我是如何提出解决方案的。
# 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']
答案 3 :(得分:0)
看起来Python可以使用一些API来解析XML文件:
http://python-guide-pt-br.readthedocs.io/en/latest/scenarios/xml/
https://www.tutorialspoint.com/python/python_xml_processing.htm
答案 4 :(得分:0)
此代码在python 3.6
下测试 import xml.etree.ElementTree as ET
name = '4.xml'
tree = ET.parse(name)
root = tree.getroot()
ditresult =[]
for child in root:
for child1 in child:
ditresult.append(child1.tag)
print (ditresult)
=============
['rank', 'year', 'gdp', 'neighbor', 'neighbor']