Python:使用minidom搜索具有特定文本的节点
我目前面临的 XML 看起来像这样:
<ID>345754</ID>
它包含在层次结构中。 我已经解析了 xml,并希望通过搜索“345754”找到 ID 节点。
I am currently faced with XML that looks like this:
<ID>345754</ID>
This is contained within a hierarchy. I have parsed the xml, and wish to find the ID node by searching on "345754".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
vartec 的答案需要更正(抱歉,我不确定我能做到这一点),它应该是这样的:有
两件事是错误的:(i)标签名称区分大小写,因此匹配
"id"
获胜不起作用,并且 (ii) 对于元素节点.nodeValue
将为None
,您需要访问包含所需值的元素节点内部的文本节点。vartec's answer needs correcting (sorry I'm not sure I can do that), it should read:
Two things were wrong with it: (i) tag names are case sensitive so matching on
"id"
won't work and (ii) for an element node.nodeValue
will beNone
, you need access to the text nodes that is inside the element node which contains the value you want.另请参阅:
See also: