2017-06-06 66 views
1

這是我的XML文件如何使用python獲取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

好測試,你嘗試過什麼? – amarnath

回答

2

使用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

解決方案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
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解析器。但我知道,「試試看」是最好的老師。

這些是步驟,我如何提出解決方案。

# 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'] 
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'] 
相關問題