Python XML解析问题



我有一个XML,如下所示,并使用python minidom来解析build.xml。我正在尝试下面的python代码来解析和检索"名称"one_answers"值"标签。我试图检索"SE_CONFIG","SE_ARCH","PREBUILDID"的值,它们具有各自的值install-csu,macosx,prebuild_7701。

面临以下挑战。

  1. 什么是更好的python方式来检索各自的名称和值对
  2. 如果没有"value",我该如何捕获异常

    <?xml version='1.0' encoding='UTF-8'?>
    <build>
      <actions>
        <hudson.model.ParametersAction>
          <parameters>
            <hudson.model.StringParameterValue>
              <name>StartFrom</name>
              <description>&lt;h3&gt;: Trigger downstreamfor this platform&lt;br&gt;</description>
              <value>Fetch_Source</value>
            </hudson.model.StringParameterValue>
            <hudson.model.StringParameterValue>
              <name>SE_CONFIG</name>
              <description></description>
              <value>install-csu</value>
            </hudson.model.StringParameterValue>
            <hudson.model.StringParameterValue>
              <name>EMAIL_RCPT</name>
              <description>Please enter your email address.</description>
              <value></value>
            </hudson.model.StringParameterValue>
            <hudson.model.StringParameterValue>
              <name>SE_ARCH</name>
              <description></description>
              <value>macosx</value>
            </hudson.model.StringParameterValue>
            <hudson.model.StringParameterValue>
              <name>PREBUILDID</name>
              <description></description>
              <value>prebuild_7701</value>
            </hudson.model.StringParameterValue>
            <hudson.model.StringParameterValue>
              <name>RE_DESCRIPTION</name>
              <description></description>
              <value></value>
            </hudson.model.StringParameterValue>
            <hudson.model.StringParameterValue>
              <name>BUILD_PRODUCT</name>
              <description></description>
              <value>release</value>
            </hudson.model.StringParameterValue>
          </parameters>
        </hudson.model.ParametersAction>  
      </actions>
      <number>8065</number>
      <result>SUCCESS</result>
      <duration>3652965</duration>
      <charset>US-ASCII</charset>
      <keepLog>false</keepLog>
      <workspace>/Users/someuser/workspace/build-mac</workspace>
      <hudsonVersion>3.2.1</hudsonVersion>
      <scm class="hudson.scm.NullChangeLogParser"/>
      <culprits/>
    </build>
    

    import xml.dom.minidom
    DOMTree=xml.dom.minidom.parse("build.xml")
    collection=DOMTree.documentElement
    string_par=collection.getElementsByTagName("hudson.model.StringParameterValue")
    for each_node in string_par:
       print each_node.getElementsByTagName('name')[0].childNodes[0].nodeValue
       print each_node.getElementsByTagName('value')[0].childNodes[0].nodeValue
    StartFrom
    Fetch_Source
    SE_CONFIG
    install-csu
    EMAIL_RCPT
    Traceback (most recent call last):
    File "<stdin>", line 3, in <module
    IndexError: list index out of range

既然您问是否有其他方法,您可以尝试使用xml.etree.ElementTree

下面的链接中有一个很酷的例子,标签可以在for循环中定义:

http://chimera.labs.oreilly.com/books/1230000000393/ch06.html _solution_96

通过ElementTree完成

    doc =xml.etree.ElementTree.parse('build.xml')
    for node in doc.iter('hudson.model.StringParameterValue'):
        print str(node.find('name').text) + 't' + str(node.find('value').text)

相关内容

  • 没有找到相关文章

最新更新