【问题标题】:ElementTree parsing the next tag in PythonElementTree 在 Python 中解析下一个标签
【发布时间】:2013-12-08 04:11:42
【问题描述】:

我有以下格式的 XML:

<id>33000</id>
<url>www.google.com</url>
<tag>website</tag>

但是,有时会有多个标签,例如:

<id>33000</id>
<url>www.google.com</url>
<tag>website</tag>
<tag>search</tag>

我的代码如下:

tree = ET.parse('unified.xml')
root = tree.getroot()
for child in root.findall('object'):
    tag = child.find('tag').text
    print tag

但是,这样我似乎无法打印第二个标签,即“搜索”。有没有办法检查是否有多个具有相同名称的标签值并打印/获取所有标签?

【问题讨论】:

    标签: python xml-parsing elementtree


    【解决方案1】:

    和你对根标签做的一样吗?

    tree = ET.parse('unified.xml')
    root = tree.getroot()
    for child in root.findall('object'):
        for tag in child.findall('tag'):
            print tag.text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多