我有一个XML转储文件,我想在第一次出现' ETH0_IP'时解析。然而,cdata领域正在抛弃我。最终返回“无”#39;还有其他IP出现在文件中,但我并不关心这些。
到目前为止我有这样的事情:
q = etree.parse(outputfile)
fileoutputip = q.findtext("ETH0_IP")
这是XML:
<VM>
<ID>####</ID>
<UID>0</UID>
<GID>0</GID>
<UNAME>####</UNAME>
<GNAME>###</GNAME>
<NAME>###</NAME>
<PERMISSIONS>
<OWNER_U>1</OWNER_U>
<OWNER_M>1</OWNER_M>
<OWNER_A>0</OWNER_A>
<GROUP_U>0</GROUP_U>
<GROUP_M>0</GROUP_M>
<GROUP_A>0</GROUP_A>
<OTHER_U>0</OTHER_U>
<OTHER_M>0</OTHER_M>
<OTHER_A>0</OTHER_A>
</PERMISSIONS>
<LAST_POLL>1461191030</LAST_POLL>
<STATE>3</STATE>
<LCM_STATE>3</LCM_STATE>
<PREV_STATE>3</PREV_STATE>
<PREV_LCM_STATE>3</PREV_LCM_STATE>
<RESCHED>0</RESCHED>
<STIME>1461189864</STIME>
<ETIME>0</ETIME>
<DEPLOY_ID>###</DEPLOY_ID>
<MEMORY>###</MEMORY>
<CPU>0</CPU>
<NET_TX>1000</NET_TX>
<NET_RX>73254</NET_RX>
<TEMPLATE>
<AUTOMATIC_REQUIREMENTS><![CDATA[!(PUBLIC_CLOUD = YES)]]></AUTOMATIC_REQUIREMENTS>
<CONTEXT>
<DISK_ID><![CDATA[1]]></DISK_ID>
<ETH0_DNS><![CDATA[####]]></ETH0_DNS>
<ETH0_GATEWAY><![CDATA[###]]></ETH0_GATEWAY>
<ETH0_IP><![CDATA[10.**.***.**]]></ETH0_IP>
答案 0 :(得分:1)
q.findtext("ETH0_IP")
会尝试直接在树中的根元素下找到ETH0_IP
元素,在这种情况下,它会生成None
,因为VM
}没有直接的ETH0_IP
孩子。
您可以通过提供XPath表达式(在树中的任何位置查找ETH0_IP
元素)到find()
或findtext()
,或使用xpath()
方法来解决此问题直接:
>>> from lxml import etree
>>>
>>> tree = etree.parse("input.xml")
>>> print(tree.find(".//ETH0_IP").text)
10.**.***.**
>>> print(tree.findtext(".//ETH0_IP"))
10.**.***.**
>>> print(tree.xpath("//ETH0_IP")[0].text)
10.**.***.**