Reputation: 183
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.
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><h3>: Trigger downstreamfor this platform<br></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
Upvotes: 0
Views: 110
Reputation: 183
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)
Upvotes: 1
Reputation: 306
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.
Upvotes: 3