Python XML 解析问题
我有一个如下所示的 xml 文件:
<!DOCTYPE ROOT SYSTEM "zombie.dtd">
<ROOT>
<row>
<field name="buildid">26960</field>
<field name="cast(status as char)">Filesystem 1K-blocks Used Available Use% Mounted on
C:cygwinin 285217976 88055920 197162056 31% /usr/bin
C:cygwinlib 285217976 88055920 197162056 31% /usr/lib
C:cygwin 285217976 88055920 197162056 31% /
c: 285217976 88055920 197162056 31% /cygdrive/c
d: 285217976 88055920 197162056 31% /cygdrive/d</field>
<field name="eventtime">2011-05-25 16:26:09</field>
<field name="schedulerid">13</field>
</row>
我正在尝试解析各个字段(buildid、status、eventtime 等),但我所有函数调用的结果都没有得到任何结果,这就是我正在做的事情:
log("Parsing XML file...")
try:
xml = ET.parse(xml_file)
except Exception, inst:
print "Unexpected error opening %s: %s" % (file, inst)
sys.exit(1)
log("Parsing Complete")
#store the root of the element tree
root = xml.getroot()
zombies = []
for zombie in root.findall('row/'):
#read the zombie data
buildID = zombie.get('buildid')
print buildID
status = zombie.get('cast(status as char')
print status
eventTime = zombie.get('eventtime')
print eventTime
schedulerID = zombie.get('schedulerid')
print schedulerID
#create a zombie object
#z = Zombie(buildID, status, eventTime, schedulerID)
#zombies.append(z)
有人可以告诉我我做错了什么吗?
谢谢
I have an xml file that looks like this:
<!DOCTYPE ROOT SYSTEM "zombie.dtd">
<ROOT>
<row>
<field name="buildid">26960</field>
<field name="cast(status as char)">Filesystem 1K-blocks Used Available Use% Mounted on
C:cygwinin 285217976 88055920 197162056 31% /usr/bin
C:cygwinlib 285217976 88055920 197162056 31% /usr/lib
C:cygwin 285217976 88055920 197162056 31% /
c: 285217976 88055920 197162056 31% /cygdrive/c
d: 285217976 88055920 197162056 31% /cygdrive/d</field>
<field name="eventtime">2011-05-25 16:26:09</field>
<field name="schedulerid">13</field>
</row>
and I am trying to parse out the individual fields (buildid, status, eventtime, etc) but I am getting none as the result of all of my function calls, here is what I am doing:
log("Parsing XML file...")
try:
xml = ET.parse(xml_file)
except Exception, inst:
print "Unexpected error opening %s: %s" % (file, inst)
sys.exit(1)
log("Parsing Complete")
#store the root of the element tree
root = xml.getroot()
zombies = []
for zombie in root.findall('row/'):
#read the zombie data
buildID = zombie.get('buildid')
print buildID
status = zombie.get('cast(status as char')
print status
eventTime = zombie.get('eventtime')
print eventTime
schedulerID = zombie.get('schedulerid')
print schedulerID
#create a zombie object
#z = Zombie(buildID, status, eventTime, schedulerID)
#zombies.append(z)
can someone tell me what I am doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
zombie
是一个|
元素,而不是您想要的
元素。当您对其调用get()
时,您正在尝试获取|
元素上的属性,但您想要的是元素的节点值>
具有该 id 的元素。如果您有 Python 2.7,则可以使用 ElementTree 的部分 XPath 支持来查找正确的元素:
Your
zombie
is a<row>
element, not the<field>
element that you want. When you callget()
on it, you are trying to get an attribute on the<row>
element, but what you want is the node value of the<field>
element with that id.If you have Python 2.7, you can use ElementTree's partial XPath support to find the correct element:
您应该获取僵尸的
text
属性,而不是使用它的get()
方法。这将为您提供
标记内的内容。另外,如果您使用ElementTree,请看一下
lxml
模块,它通常比ET更好,并且完全支持XPath 1.0 表达式(您可以使用它轻松地按名称查找元素)。You should be getting the
text
property of the zombie, not using itsget()
method. That will give you the content inside de<field>
tag.Also, if you are using ElementTree, take a look at the
lxml
module, which is generally better than ET, and fully supports XPath 1.0 expressions (which you can use to easily find the elements by their name).