Python:XML解析xml.dom.minidom迭代集合.getElementsByTagNam

2024-10-02 12:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些xml我正试图用Python2.7解析。XML的格式如下。在代码中(也包括在下面),如果我尝试使用collection.getAttribute('ProjectId')打印,我会得到id号,但是一旦我将collection分配给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>

代码:

^{pr2}$

Tags: 代码nameinfomessagexmllevelcollectionlogentry
1条回答
网友
1楼 · 发布于 2024-10-02 12:30:14

由于collection.getElementsByTagName("SummaryReport")不返回任何内容,因此没有输出:

>>> tags = collection.getElementsByTagName("SummaryReport")
>>> print(tags)
[]

这是有意义的,因为collection已经引用了SummaryReport元素,并且它没有同名的后代元素。在

更新:

简单的for循环可以很好地遍历Level元素并打印值,例如:

^{pr2}$

相关问题 更多 >

    热门问题