python - 解析带有前缀标签的 .xml? xml.etree.元素树

标签 python xml xml.etree

我可以读取标签,除非有前缀。我没有幸运地搜索 SO 以查找以前的问题。

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

<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 

我一直在使用文档,但仍停留在“前缀”部分。

最佳答案

下面是一个使用 ElementTree 的 XML 命名空间的例子:

>>> 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

关于python - 解析带有前缀标签的 .xml? xml.etree.元素树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949092/

相关文章:

java - 尝试隐藏 View /布局时出现 NullPointerException

java - 如何通过命名空间获取所有具有相同标签名的元素?

c# - 使用 C# 从 XML 文件中删除数据?

python - Elementtree 转储给出错误答案

python pty 模块 - 缓冲区挂起?

python - 有效地将函数应用于numpy数组中的球形邻域

python - NumPy hstack 抛出 "ValueError: all the input arrays must have same number of dimensions?"

Python - 如何仅将两个列表中的某些数字相乘

python - 使用 etree 从文件中解析 xml 在读取字符串时有效,但在读取文件时则无效