我有以下XML。
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="10" failures="0" disabled="0" errors="0" time="0.001" name="AllTests">
<testsuite name="TestOne" tests="5" failures="0" disabled="0" errors="0" time="0.001">
<testcase name="DefaultConstructor" status="run" time="0" classname="TestOne" />
<testcase name="DefaultDestructor" status="run" time="0" classname="TestOne" />
<testcase name="VHDL_EMIT_Passthrough" status="run" time="0" classname="TestOne" />
<testcase name="VHDL_BUILD_Passthrough" status="run" time="0" classname="TestOne" />
<testcase name="VHDL_SIMULATE_Passthrough" status="run" time="0.001" classname="TestOne" />
</testsuite>
</testsuites>
问:如何找到节点<testcase name="VHDL_BUILD_Passthrough" status="run" time="0" classname="TestOne" />
?我找到了函数tree.find()
,但是这个函数的参数似乎是元素名。
我需要根据属性找到节点:name = "VHDL_BUILD_Passthrough" AND classname="TestOne"
。
这取决于你使用的是什么版本。如果您有ElementTree 1.3+(包括在Python 2.7标准库中),那么可以使用基本的xpath表达式,如described in the docs,如
[@attrib='value']
:不幸的是,如果您使用的是ElementTree的早期版本(1.2,包含在python 2.5和2.6的标准库中),那么您就不能使用这种方便,需要对自己进行过滤。
您必须遍历您拥有的
<testcase />
元素,如下所示:相关问题 更多 >
编程相关推荐