【问题标题】:How to parse XML in python using ElementTree (find content of a tag)如何使用 ElementTree 在 python 中解析 XML(查找标签的内容)
【发布时间】:2017-01-16 14:31:12
【问题描述】:

我正在尝试编写一个 python 脚本来查找 XML 标记的内容并将找到的任何行打印回给我。

到目前为止,我有 __

import os
import sys

import xml.etree.ElementTree as ET

for fn in os.listdir(".") :
    if fn.endswith(".xml") :
        if not os.path.exists(fn) :
            sys.exit(fn+' does not exist')
        doc = ET.parse(fn)
**      for e in doc.findall( ".//ChangeView/VideoPlaying/text() = 'true'" ) :
            print(fn+' has '+ET.tostring(e))

对于具有

的 XML 文档
<ChangeView>
    <VideoPlaying>true</VideoPlaying>
</ChangeView>

主要问题是带**的那一行,检测VideoPlaying标签内容的语法是什么?

谢谢

【问题讨论】:

    标签: python xml parsing elementtree


    【解决方案1】:

    试试

    for e in doc.findall('.//ChangeView/VideoPlaying'):
        if e.text == 'true':
            print(ET.tostring(e))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多