【发布时间】:2014-09-09 19:35:56
【问题描述】:
我一直在尝试解析 xml 文件列表。我想打印特定的值,例如 userName 值。
<?xml version="1.0" encoding="utf-8"?>
<Drives clsid="{8FDDCC1A-0C3C-43cd-A6B4-71A6DF20DA8C}"
disabled="1">
<Drive clsid="{935D1B74-9CB8-4e3c-9914-7DD559B7A417}"
name="S:"
status="S:"
image="2"
changed="2007-07-06 20:57:37"
uid="{4DA4A7E3-F1D8-4FB1-874F-D2F7D16F7065}">
<Properties action="U"
thisDrive="NOCHANGE"
allDrives="NOCHANGE"
userName=""
cpassword=""
path="\\scratch"
label="SCRATCH"
persistent="1"
useLetter="1"
letter="S"/>
</Drive>
</Drives>
我的脚本工作正常,收集 xml 文件列表等。但是下面的功能是打印相关值。我正在尝试按照post 中的建议实现这一目标。但是我显然做错了什么,因为我收到错误提示elm 对象没有属性text。任何帮助将不胜感激。
当前代码
from lxml import etree as ET
def read_files(files):
for fi in files:
doc = ET.parse(fi)
elm = doc.find('userName')
print elm.text
【问题讨论】: