我有这个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>
现在,我要解析这个并获取它的属性值。例如,我想获取uuid
字段。那么,用Python来获取它的正确方法应该是什么呢?
etree,用lxml可能:
这里有一个lxml片段,它提取属性和元素文本(您的问题有点含糊不清,所以我将两者都包括在内):
你问(在对Ali Afshar的回答的评论中)minidom是否是一个好的选择。下面是使用minidom的等效代码;请自己判断哪个更好:
lxml在我看来是赢家。
XML
Python:
相关问题 更多 >
编程相关推荐