XML.Etree Get get Tag,其具有特定属性的孩子
我有以下XML文件:
<node id="1416646243" />
<node id="1416646244">
<tag k="crossing" v="unregulated" />
</node>
<node id="1416646245">
<tag k="crossing" v="traffic_signals" />
</node>
我想选择&lt; node&gt;
标签,其中包含a &lt; tag&gt;
带有atteribute v =“ copery_signals” < /code>。
但是,如果我使用以下代码,则在返回中获得&lt; tag&gt;
标签。
root.find('.// node/tag [@v =“ crigings_signals”]')
,据我所知,xml.etree
不提供一个获得父母的方法。
我如何真正获得节点
标签?
I have the following xml file:
<node id="1416646243" />
<node id="1416646244">
<tag k="crossing" v="unregulated" />
</node>
<node id="1416646245">
<tag k="crossing" v="traffic_signals" />
</node>
I want to select the <node>
tag which contains a <tag>
tag with attribute v="traffic_signals"
.
However if I use the following code, I get the <tag>
tag in return.
root.find('.//node/tag[@v="traffic_signals"]')
And as far as i know, xml.etree
doesn't provide a way to get parent.
How can I actually get the node
tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是很好 - 但它
起作用
Not very eficcient - but it works
output
在这里,您的解决方案可以使用“ // parent [./direct_child]”或“ // parent [.//children_of_child]“结果元素”元素是父母,您可以使用子元素检查元素。
Here your solution, you can check element with sub-element inside "//parent[./direct_child]" or "//parent[.//children_of_child]" result element will be the parent
有点骇客,但是您可以使用
..
在与孩子匹配后返回父母。It's a little hacky, but you can use
..
to go back up to the parent after matching the child.