Python XML 解析问题

发布于 2024-11-15 03:10:20 字数 1596 浏览 1 评论 0原文

我有一个如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-11-22 03:10:20

您的 zombie 是一个 元素,而不是您想要的 元素。当您对其调用 get() 时,您正在尝试获取 元素上的属性,但您想要的是 元素的节点值> 具有该 id 的元素。

如果您有 Python 2.7,则可以使用 ElementTree 的部分 XPath 支持来查找正确的元素:

build_id = zombie.find('field[name="buildid"]').text

Your zombie is a <row> element, not the <field> element that you want. When you call get() 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:

build_id = zombie.find('field[name="buildid"]').text
心的位置 2024-11-22 03:10:20

您应该获取僵尸的 text 属性,而不是使用它的 get() 方法。这将为您提供 标记内的内容。

另外,如果您使用ElementTree,请看一下lxml模块,它通常比ET更好,并且完全支持XPath 1.0 表达式(您可以使用它轻松地按名称查找元素)。

You should be getting the text property of the zombie, not using its get() 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文