黑松山资源网 Design By www.paidiu.com
问题
你想解析某个XML文档,文档中使用了XML命名空间。
解决方案
考虑下面这个使用了命名空间的文档:
<"1.0" encoding="utf-8"?> <top> <author>David Beazley</author> <content> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello World</title> </head> <body> <h1>Hello World!</h1> </body> </html> </content> </top>
如果你解析这个文档并执行普通的查询,你会发现这个并不是那么容易,因为所有步骤都变得相当的繁琐。
> # Some queries that work > doc.findtext('author') 'David Beazley' > doc.find('content') <Element 'content' at 0x100776ec0> > # A query involving a namespace (doesn't work) > doc.find('content/html') > # Works if fully qualified > doc.find('content/{http://www.w3.org/1999/xhtml}html') <Element '{http://www.w3.org/1999/xhtml}html' at 0x1007767e0> > # Doesn't work > doc.findtext('content/{http://www.w3.org/1999/xhtml}html/head/title') > # Fully qualified > doc.findtext('content/{http://www.w3.org/1999/xhtml}html/' ... '{http://www.w3.org/1999/xhtml}head/{http://www.w3.org/1999/xhtml}title') 'Hello World' >
你可以通过将命名空间处理逻辑包装为一个工具类来简化这个过程:
class XMLNamespaces: def __init__(self, **kwargs): self.namespaces = {} for name, uri in kwargs.items(): self.register(name, uri) def register(self, name, uri): self.namespaces[name] = '{'+uri+'}' def __call__(self, path): return path.format_map(self.namespaces)
通过下面的方式使用这个类:
> ns = XMLNamespaces(html='http://www.w3.org/1999/xhtml') > doc.find(ns('content/{html}html')) <Element '{http://www.w3.org/1999/xhtml}html' at 0x1007767e0> > doc.findtext(ns('content/{html}html/{html}head/{html}title')) 'Hello World' >
讨论
解析含有命名空间的XML文档会比较繁琐。 上面的 XMLNamespaces
仅仅是允许你使用缩略名代替完整的URI将其变得稍微简洁一点。
很不幸的是,在基本的 ElementTree
解析中没有任何途径获取命名空间的信息。 但是,如果你使用 iterparse()
函数的话就可以获取更多关于命名空间处理范围的信息。例如:
> from xml.etree.ElementTree import iterparse > for evt, elem in iterparse('ns2.xml', ('end', 'start-ns', 'end-ns')): ... print(evt, elem) ... end <Element 'author' at 0x10110de10> start-ns ('', 'http://www.w3.org/1999/xhtml') end <Element '{http://www.w3.org/1999/xhtml}title' at 0x1011131b0> end <Element '{http://www.w3.org/1999/xhtml}head' at 0x1011130a8> end <Element '{http://www.w3.org/1999/xhtml}h1' at 0x101113310> end <Element '{http://www.w3.org/1999/xhtml}body' at 0x101113260> end <Element '{http://www.w3.org/1999/xhtml}html' at 0x10110df70> end-ns None end <Element 'content' at 0x10110de68> end <Element 'top' at 0x10110dd60> > elem # This is the topmost element <Element 'top' at 0x10110dd60> >
最后一点,如果你要处理的XML文本除了要使用到其他高级XML特性外,还要使用到命名空间, 建议你最好是使用 lxml 函数库来代替 ElementTree 。 例如,lxml 对利用DTD验证文档、更好的XPath支持和一些其他高级XML特性等都提供了更好的支持。 这一小节其实只是教你如何让XML解析稍微简单一点。
以上就是Python利用命名空间解析XML文档的详细内容,更多关于Python命名空间解析XML文档的资料请关注其它相关文章!
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月05日
2024年10月05日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]