2012-09-05 72 views
3

它的屬性值,我有這樣的XML文件讀取XML文件,並獲取在Python

<domain type='kmc' id='007'> 
    <name>virtual bug</name> 
    <uuid>66523dfdf555dfd</uuid> 
    <os> 
    <type arch='xintel' machine='ubuntu'>hvm</type> 
    <boot dev='hd'/> 
    <boot dev='cdrom'/> 
    </os> 
    <memory unit='KiB'>524288</memory> 
    <currentMemory unit='KiB'>270336</currentMemory> 
    <vcpu placement='static'>10</vcpu> 

現在我想分析這個並獲取其屬性值。例如,我想獲取uuid字段。那麼在Python中獲取它的正確方法是什麼?

+6

你有什麼試過的?使用Google搜索「python xml」會產生相當多的真正有用的結果,這些結果應該指向正確的方向。 – Blender

+0

有很多例子,但沒有指出我想去的方向。我想獲取屬性值。我看到的例子是轉換爲XML文件或轉換形式的XML文件 –

回答

17

下面是一個lxml片段提取的屬性以及元素文本(你的問題有點含糊不清哪一個你需要的,所以我既包括):

from lxml import etree 
doc = etree.parse(filename) 

memoryElem = doc.find('memory') 
print memoryElem.text  # element text 
print memoryElem.get('unit') # attribute 

你問(在評論阿里Afshar的答案) r minidom2.x3.x)是一個很好的選擇。這是使用minidom的等效代碼;爲自己判斷哪個更好:

import xml.dom.minidom as minidom 
doc = minidom.parse(filename) 

memoryElem = doc.getElementsByTagName('memory')[0] 
print ''.join([node.data for node in memoryElem.childNodes]) 
print memoryElem.getAttribute('unit') 

lxml看起來像是我的贏家。

+1

這個方法也與Python 2和3包含的['xml.etree.ElementTree'](https://docs.python.org/library/xml.etree.elementtree.html)兼容。 –

1

etree,與lxml可能:

root = etree.XML(MY_XML) 
uuid = root.find('uuid') 
print uuid.text 
+0

不會是minidom是一個不錯的選擇。你怎麼看 –

0

我會用LXML和使用xpath //UUID

0

其他人可以告訴你如何與Python標準庫做解析出來。我推薦我自己的小型圖書館,這使得這是一個非常簡單的過程。

>>> obj = xml2obj.xml2obj("""<domain type='kmc' id='007'> 
... <name>virtual bug</name> 
... <uuid>66523dfdf555dfd</uuid> 
... <os> 
... <type arch='xintel' machine='ubuntu'>hvm</type> 
... <boot dev='hd'/> 
... <boot dev='cdrom'/> 
... </os> 
... <memory unit='KiB'>524288</memory> 
... <currentMemory unit='KiB'>270336</currentMemory> 
... <vcpu placement='static'>10</vcpu> 
... </domain>""") 
>>> obj.uuid 
u'66523dfdf555dfd' 

http://code.activestate.com/recipes/534109-xml-to-python-data-structure/

11

XML

<data> 
    <items> 
     <item name="item1">item1</item> 
     <item name="item2">item2</item> 
     <item name="item3">item3</item> 
     <item name="item4">item4</item> 
    </items> 
</data> 

的Python:

from xml.dom import minidom 
xmldoc = minidom.parse('items.xml') 
itemlist = xmldoc.getElementsByTagName('item') 
print "Len : ", len(itemlist) 
print "Attribute Name : ", itemlist[0].attributes['name'].value 
print "Text : ", itemlist[0].firstChild.nodeValue 
for s in itemlist : 
    print "Attribute Name : ", s.attributes['name'].value 
    print "Text : ", s.firstChild.nodeValue 
0

上面的XML沒有關閉標籤,它會給

etree parse error: Premature end of data in tag

正確的XML是:

<domain type='kmc' id='007'> 
    <name>virtual bug</name> 
    <uuid>66523dfdf555dfd</uuid> 
    <os> 
    <type arch='xintel' machine='ubuntu'>hvm</type> 
    <boot dev='hd'/> 
    <boot dev='cdrom'/> 
    </os> 
    <memory unit='KiB'>524288</memory> 
    <currentMemory unit='KiB'>270336</currentMemory> 
    <vcpu placement='static'>10</vcpu> 
</domain>