2013-01-04 79 views
0

我有一個像下面的XML文件:Python的XML解析 - multisections

<item> 
    <global> 

    <option id="123b25-1323-2f"> 
     <name>Bla</name> 
     <number></number> 
    </option> 
    <option id="aeb12f-91b3-57"> 
     <name>Foo</name> 
     <number>92309</number> 
    </option> 

    <section id="aeee72-0965-66"> 
     <name>alb</name> 
     <number></number> 
    </section> 
    <section id="928374-11b3-51"> 
     <name>oof</name> 
     <number>92309</number> 
    </section> 

    </global> 
</item> 

什麼是構建一個字典FO例如最好的辦法選項和部分與python2.7和一個適當的模塊。
示例代碼:

root = XMLTree(xml) # xml ist file or string 
global = root.getSubsection('global') 
options = global.getItems('option') 
sections = global.getItems('section') 

print options 

我希望輸出這樣的:

=> {'id-123b25-1323-2f': {'name': 'Bla', 'number': ''}, 'id-aeb12f-91b3-57': {'name': 'Foo', 'number': '92309'}} 

回答

1

ElementTree是非常合適的標準庫模塊。這裏有一個建議(Python 2.7版):

from xml.etree import ElementTree as ET 

def get_items(elements): 
    D = {elem.get("id"): dict((child.tag, child.text) for child in elem) 
     for elem in elements} 
    return D 

tree = ET.parse("item.xml") 
options = tree.findall(".//option") 
sections = tree.findall(".//section") 

print "options:" 
print get_items(options) 
print "sections:" 
print get_items(sections) 

輸出:

options: 
{'aeb12f-91b3-57': {'name': 'Foo', 'number': '92309'}, '123b25-1323-2f': {'name': 'Bla', 'number': ''}} 
sections: 
{'928374-11b3-51': {'name': 'oof', 'number': '92309'}, 'aeee72-0965-66': {'name': 'alb', 'number': ''}} 
+0

,看起來完全像什麼即時尋找。我目前無法嘗試,但提前致謝 – HappyHacking

1

您可以使用xml.dom.minidom解析xml字符串並提取元素以創建字典。下面是minidom命名一個例子

from xml.dom.minidom import parseString 
dom = parseString(data) #xml dom object from xml 
def getItems(node): 
    """dom parser and xml generator""" 
    return {node.getAttribute('id'): 
       dict((e.nodeName, e.firstChild.data) 
         for e in node.childNodes if e.nodeType == dom.ELEMENT_NODE) 
      for node in node } 

options = dom.getElementsByTagName('option') 
sections = dom.getElementsByTagName('section') 
getItems(options) 
{u'aeb12f-91b3-57': {u'name': u'Foo', u'number': u'92309'}, u'123b25-1323-2f': {u'name': u'Bla', u'number': u''}} 
getItems(sections) 
{u'928374-11b3-51': {u'name': u'oof', u'number': u'92309'}, u'aeee72-0965-66': {u'name': u'alb', u'number': u''}} 
1
import lxml.etree as et 

doc=et.fromstring(xml) 

def getItems(doc,name): 
    d={} 
    for elem in doc.xpath('.//{0}'.format(name)): 
     attr=elem.xpath('.//@id')[0] 
     items=[(i.tag, i.text) for i in elem.xpath('.//*')] 
     d[attr]={k:v for k,v in items} 
    return d 
print getItems(doc,'option') 
print getItems(doc,'section') 

輸出:

{'aeb12f-91b3-57': {'name': 'Foo', 'number': '92309'}, '123b25-1323-2f': {'name': 'Bla', 'number': ''}} 
{'928374-11b3-51': {'name': 'oof', 'number': '92309'}, 'aeee72-0965-66': {'name': 'alb', 'number': ''}}