Python XML 处理与输入源抽象全解析
1. XML 元素搜索
在 XML 处理中,搜索特定元素是常见操作。以下示例展示了如何在 XML 文档中搜索 <p> 元素。
1.1 在特定元素内搜索
# 假设 firstref 是一个 XML 元素对象
print firstref.toxml()
<ref id="bit">
<p>0</p>
<p>1</p>
</ref>
plist = firstref.getElementsByTagName("p")
print plist
[<DOM Element: p at 136140116>, <DOM Element: p at 136142172>]
print plist[0].toxml()
<p>0</p>
print plist[1].toxml()
<p>1</p>
上述代码中, firstref.getElementsByTagName("p") 方法用于在 firstref 元素内搜索所有 <p> 元素,返回一个元素列表。
1.2 在整个文档中搜索
# 假设 xmldoc 是整个 XML 文档的根对象
plist
超级会员免费看
订阅专栏 解锁全文
15万+

被折叠的 条评论
为什么被折叠?



