解析带有前缀的标签上的.xml? xml.etree.ElementTree [英] parse .xml with prefix's on tags? xml.etree.ElementTree

查看:116
本文介绍了解析带有前缀的标签上的.xml? xml.etree.ElementTree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以读取标签,除非有前缀.我没有运气来寻找上一个问题.

I can read tags, except when there is a prefix. I'm not having luck searching SO for a previous question.

我需要阅读media:content.我尝试了image = node.find("media:content"). Rss输入:

I need to read media:content. I tried image = node.find("media:content"). Rss input:

<channel>
  <title>Popular  Photography in the last 1 week</title>
  <item>
    <title>foo</title>
    <media:category label="Miscellaneous">photography/misc</media:category>
    <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/>
  </item>
  <item> ... </item>
</channel>

我可以阅读同级标签title.

from xml.etree import ElementTree
with open('cache1.rss', 'rt') as f:
    tree = ElementTree.parse(f)

for node in tree.findall('.//channel/item'):
    title =  node.find("title").text 

我一直在使用文档,但停留在前缀"部分.

推荐答案

下面是将XML名称空间与 ElementTree 一起使用的示例:

Here's an example of using XML namespaces with ElementTree:

>>> x = '''\
<channel xmlns:media="http://www.w3.org/TR/html4/">
  <title>Popular  Photography in the last 1 week</title>
  <item>
    <title>foo</title>
    <media:category label="Miscellaneous">photography/misc</media:category>
    <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/>
  </item>
  <item> ... </item>
</channel>
'''
>>> node = ElementTree.fromstring(x)
>>> for elem in node.findall('item/{http://www.w3.org/TR/html4/}category'):
        print elem.text


photography/misc

这篇关于解析带有前缀的标签上的.xml? xml.etree.ElementTree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆