Python:使用xml.dom.minidom进行XML解析 - 迭代collection.getElementsByTagName

时间:2015-09-11 03:04:15

标签: python xml

我有一些xml我试图使用Python 2.7进行解析。 XML的格式如下。在代码中(也包括在下面),如果我尝试使用collection.getAttribute('ProjectId')进行打印,我会得到id号,但是一旦我将集合分配给tags然后通过循环运行它,我就不会得到输出(没有错误信息)。有线索吗?

XML:

<SummaryReport ProjectId="37f8d135-1f1d-4e57-9b7d-b084770c6bf5" EntityId="016fbc07-69f0-407e-b5b5-0b0b6bba4307" Status="Failed">
  <TotalCount>0</TotalCount>
  <SuccessfulCount>0</SuccessfulCount>
  <FailedCount>0</FailedCount>
  <StartUtcTime>2015-09-09T16:43:11.810715Z</StartUtcTime>
  <EndUtcTime>2015-09-09T16:43:44.5418427Z</EndUtcTime>
  <IsIncremental>false</IsIncremental>
  <OnDemand>true</OnDemand>
  <TrackingId>c0972936-c8b6-4cdb-b089-d08c6f9702aa</TrackingId>
  <Message>An error occurred during content building: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index</Message>
  <LogEntries>
    <LogEntry>
      <Level>Info</Level>
      <LogCodeType>System</LogCodeType>
      <LogCode>PhaseSucceedInfo</LogCode>
      <Name>Phase</Name>
      <Message>'Load Metadata' succeeded in 00:00:00.1905878 seconds.</Message>
      <Anchor>Info_7333babe-fc51-4b45-9167-bf263e7babcb</Anchor>
    </LogEntry>
    <LogEntry>
     <Level>Info</Level>
     <LogCodeType>System</LogCodeType>
     <LogCode>PublishRequest</LogCode>
     <Name>PublishTocAndArticleInit</Name>
     <Message>'Load Metadata' succeeded in 00:00:01.1905878 seconds.</Message>
     <Anchor>Info_51c10e71-d99a-49f9-b4aa-d83dc273426a</Anchor>
     </LogEntry>
  </LogEntries> 
</SummaryReport>

代码:

#!/usr/bin/python

from xml.dom.minidom import parse
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("file.xml")
collection = DOMTree.documentElement

#Get all the tags under summaryreport
tags = collection.getElementsByTagName("SummaryReport")


#print tag info
for tag in tags:
    print '*******Tag Info************'

    print 'Project Id: %s' % tag.getAttribute('ProjectId')

1 个答案:

答案 0 :(得分:0)

由于 $(document).ready(function() { var counter = 1; $.getJSON('http://www.passionla.com/app/blog-data.php', function(data) { $.each(data, function(key, value) { $('.blog').append("<div class='post animated slideInRight "+ counter++ +"' style=\"background:url('"+value.header+"') no-repeat; background-size: 100% 100%\"><div class='color'><div class='wrapper'><h1>"+value.title+"</h1></div></div></div>"); }); }); }); 没有返回任何内容,因此没有输出:

collection.getElementsByTagName("SummaryReport")

这是有道理的,因为>>> tags = collection.getElementsByTagName("SummaryReport") >>> print(tags) [] 已经引用collection元素,并且它没有名称相同的后代元素。

更新:

简单SummaryReport循环可以很好地迭代for个元素并打印该值,例如:

Level