[英]Get XPATH of certain element in python (without using 'lxml')
我有一个XML(Output.xml):
<condition>
<comparison compare="and">
<comparison compare="contains">
<operand type="string"/>
<operand type="string" value="Agent"/>
</comparison>
<comparison compare="gt">
<operand type="float"/>
<operand type="int" />
</comparison>
</comparison>
</condition>
完美工作的python代码:
from lxml import etree
tree = etree.parse('Output.xml')
for condition in tree.xpath('//operand'):
print tree.getelementpath(condition)
结果:
/condition/comparison/comparison[1]/operand[1]
/condition/comparison/comparison[1]/operand[2]
/condition/comparison/comparison[2]/operand[1]
/condition/comparison/comparison[2]/operand[2]
我正在使用不支持“ LXML”模块的Apache NiFi(使用Jython)。 有什么办法可以通过xml.etree.ElementTree之类的不同模块获得相同的输出,可以从提供的xml中提取某些元素的xpath?
您可以使用xml.etree.ElementTree
import xml.etree.ElementTree
e = xml.etree.ElementTree.parse('output.xml').getroot()
root = e.tag
print(root)
for c in e.findall('.//'):
print(c.tag)
只是向您展示如何进行,(这为您提供了xml中的所有标签)
condition
comparison
comparison
operand
operand
comparison
operand
operand
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.