对于这个xml
<Departments orgID="123" name="xmllist">
<Department>
<orgID>124</orgID>
<name>A</name>
<type>type a</type>
<status>Active</status>
<Department>
<orgID>125</orgID>
<name>B</name>
<type>type b</type>
<status>Active</status>
<Department>
<orgID>126</orgID>
<name>C</name>
<type>type c</type>
<status>Active</status>
</Department>
</Department>
</Department>
<Department>
<orgID>109449</orgID>
<name>D</name>
<type>type d</type>
<status>Active</status>
</Department>
</Departments>
如何在python中使用lxml
etree
获取节点的所有父节点。在
预期输出:Input orgid=126,它将返回所有父类
^{pr2}$
使用
lxml
和XPath:使用
^{pr2}$ancestor-or-self
轴,您可以找到节点本身、父节点、祖父母。。。在使用lxml的
iterancestors()
方法。在输出:
^{pr2}$如果您实际上试图保留元素的顺序,则不能使用dict,因为您无法控制dict中的键顺序。您必须使用OrderedDict或just and数组元组:
输出:
相关问题 更多 >
编程相关推荐