2017-01-16 76 views
0

我正在嘗試編寫一個python腳本來查找XML標記的內容,並將它找回來打印回給我。如何在Python中使用ElementTree解析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)) 

對於具有

<ChangeView> 
    <VideoPlaying>true</VideoPlaying> 
</ChangeView> 

的主要問題一個XML文檔與**行,什麼是檢測的內容語法VideoPlaying標籤?

感謝

回答

0

嘗試

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