可以通过 lxml 中子元素的文本值搜索 XML 元素吗?
我有一个具有如下结构的 XML 文件:
<index>
<compound kind="file">
<name>file.c</name>
<member kind="variable"><name>foo</name></member>
<member kind="variable"><name>bar</name></member>
...
</compound>
<compound kind="file">
<name>file.h></name>
<member>...
</compound>
</index>
我需要按文件成员名称进行搜索,但我无法弄清楚是否有一种方法可以避免迭代整个树。我的解决方案目前看起来像:
for f in xmlroot.iter("compound"):
for m in f.iter("member"):
if m.find("name").text == my_var_name:
print "Found"
有没有办法使用 dict() 搜索来提高效率,因为我实际上有另一个上面的 for 循环,它会遍历变量列表来搜索,因此从性能角度来看,这真的很差。
将这两个 for 循环更改为单个 XPath 搜索会提高性能吗?
I have an XML file with a structure like this:
<index>
<compound kind="file">
<name>file.c</name>
<member kind="variable"><name>foo</name></member>
<member kind="variable"><name>bar</name></member>
...
</compound>
<compound kind="file">
<name>file.h></name>
<member>...
</compound>
</index>
I need to search by file member name but I can't figure out if there is a way that avoids iterating the entire tree. My solution currently looks like:
for f in xmlroot.iter("compound"):
for m in f.iter("member"):
if m.find("name").text == my_var_name:
print "Found"
Is there a way to use a dict() search to improve efficiency because I actually have another for loop above that that goes through the list of variables to search for so performance-wise this is really poor.
Would changing those two for loops to a single XPath search improve performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论