0

I have an XML as attached below and using python minidom to parse the build.xml. I am trying below python code to parse and retrieve the "name" and "value" tag. I am trying to retrieve the values for "SE_CONFIG","SE_ARCH","PREBUILDID" which have respective value install-csu,macosx,prebuild_7701.

Having following challenges.

  1. What is better pythonic way to retrieve respective name and value pair
  2. How should I catch the exception if there is no "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
2
  • Check the length of the childNodes list. If it's zero, you know there wasn't a value. Commented Aug 30, 2016 at 15:29
  • Thanks @JohnGordon for the suggestion. Is there a easier\better way to retrieve the name,value pair. I believe the code I written is complex and could have been lot more simpler Commented Aug 30, 2016 at 15:36

2 Answers 2

3

Since you asked if there is another way, you can try using xml.etree.ElementTree.

There is a cool example in the following link, the tags can be defined in the for loop:

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

I hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @L.Felipe I was able to get it through ElementTree as below.
1

Get it done through 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.