如何使用Python获取XML元素的内容?

时间:2016-12-15 12:21:55

标签: python xml dom

我希望12345使用NodeID获得python's DOM

这是XML:

<?xml version="1.0" ?>
<Inventory version="2.0">
    <Component Classname="DCIM_ControllerView" Key="RAID.Slot.1-1">
       <PROPERTY NAME="NodeID" TYPE="string">
         <VALUE>20161210161621</VALUE>
         <DisplayValue>20161210161621</DisplayValue>
       </PROPERTY>
    </Component>
    <Component Classname="DCIM_SystemView" Key="System.Embedded.1">
       <PROPERTY NAME="NodeID" TYPE="string">
         <VALUE>12345</VALUE>
         <DisplayValue>12345</DisplayValue>
       </PROPERTY>
    </Component>
</Inventory>

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

import xml.etree.ElementTree
e = xml.etree.ElementTree.fromstring(theXmlString) # or from file
e.findall("Component[@Key='RAID.Slot.1-1']/PROPERTY/VALUE")[0].text