【问题标题】:Python XML Parsing issuePython XML解析问题
【发布时间】:2016-08-30 15:25:26
【问题描述】:

我有一个如下附件的 XML,并使用 python minidom 来解析 build.xml。我正在尝试下面的 python 代码来解析和检索“名称”和“值”标签。我正在尝试检索具有相应值 install-csu、macosx、prebuild_7701 的“SE_CONFIG”、“SE_ARCH”、“PREBUILDID”的值。

面临以下挑战。

  1. 有什么更好的 Pythonic 方法来检索相应的名称和值对
  2. 如果没有“价值”,我应该如何捕捉异常

    <?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

【问题讨论】:

  • 检查childNodes 列表的长度。如果它为零,您就知道没有值。
  • 感谢@JohnGordon 的建议。是否有更简单\更好的方法来检索名称、值对。我相信我写的代码很复杂,本来可以更简单的

标签: python xml minidom


【解决方案1】:

既然你问有没有其他方法,你可以试试xml.etree.ElementTree

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

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

希望对你有帮助。

【讨论】:

  • 感谢@L.Felipe 我能够通过 ElementTree 得到它,如下所示。
【解决方案2】:

通过 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)

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2017-10-09
    • 2012-08-01
    • 1970-01-01
    相关资源
    最近更新 更多