Is there a way to use python to extract a value from an xml file?
Extracting a value from an xml file
Collapse
X
-
Tags: None
-
-
I can use the code below to parse the file, but how can I extract a value of a tag such as 'name' with a value and return that value to an external program?
Code:from xml.dom.minidom import parse, parseString dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name
Comment
-
this is what the contents of the xml file looks like and I want to take the value of 'name' and return that to an external program.
Code:<servicing name="color-measure-print-and-scan">
Comment
-
Originally posted by ironmonkey69this is what the contents of the xml file looks like and I want to take the value of 'name' and return that to an external program.
Code:<servicing name="color-measure-print-and-scan">
[CODE=python]elements = dom1.getElement sByTagName("ser vicing")
return elements[0].attributes['name'].value[/CODE]
If you have multiple servicing tags, just iterate through elements instead of only looking at the first index.Comment
-
My python script looks like this
Code:import xml.dom.minidom from xml.dom.minidom import parse, parseString dom1 = parse('/home/test.xml') # parse an XML file by name elements = dom1.getElementsByTagName("servicing") return elements[0].attributes['name'].value
Comment
-
Originally posted by ironmonkey69My python script looks like this
Code:import xml.dom.minidom from xml.dom.minidom import parse, parseString dom1 = parse('/home/test.xml') # parse an XML file by name elements = dom1.getElementsByTagName("servicing") return elements[0].attributes['name'].value
Try this:
[CODE=python]import xml.dom.minidom
from xml.dom.minidom import parse, parseString
dom1 = parse('/home/test.xml')
elements = dom1.getElement sByTagName("ser vicing")
name = elements[0].attributes['name'].value
print name[/CODE]Comment
-
when i run this script, it says "ImportErro r: No module named xml.dom.minidom "
Is there any other way to perform this function without using minidom?Comment
-
Originally posted by ironmonkey69when i run this script, it says "ImportErro r: No module named xml.dom.minidom "
Is there any other way to perform this function without using minidom?
As far as other ways to parse go, I have only used minidom to extract info from xml files, so I don't know of any other way to recommend.Comment
-
Originally posted by KaezarRexThat's strange that it won't import because minidom is automatically included in python. I am using 2.5, but I'm almost positive that the minidom module should work the same as in 2.0.
As far as other ways to parse go, I have only used minidom to extract info from xml files, so I don't know of any other way to recommend.Comment
-
Originally posted by ironmonkey69when i run this script, it says "ImportErro r: No module named xml.dom.minidom "
Is there any other way to perform this function without using minidom?Comment
-
-
Originally posted by ironmonkey69this is what the contents of the xml file looks like and I want to take the value of 'name' and return that to an external program.
Code:<servicing name="color-measure-print-and-scan">
Comment
-
sure
[CODE=xml]
<servicing name="alignment ">
<!-- *************** *************** ****** BEGIN OF PASS *************** *************** ************ -->
<!-- TRJ-002448 Changed to prevent the carriage from going to the secondary spittoon during printhead alignment
plots. This happens because the printmode is a mix of uni-directional and bi-directional print modes. Date 11th April 2006 -->
<begin-of-pass>
<flying-spit type="fixed">
<parameters>
<fixed mode="only-SVS"/>
</parameters>
</flying-spit>
</begin-of-pass>
<!-- *************** *************** ****** BEGIN OF PAGE *************** *************** ************ -->
<!-- Disable drop detection -->
<begin-of-page>
<drop-detection enabled="0">
</drop-detection>
</begin-of-page>
<!-- *************** *************** ****** END OF JOB *************** *************** ************ -->
<!-- Disable drop detection -->
<end-of-job>
<drop-detection enabled="0">
</drop-detection>
</end-of-job>
</servicing>
[/CODE]Comment
-
Originally posted by ironmonkey69sure
[CODE=xml]
<servicing name="alignment ">
<!-- *************** *************** ****** BEGIN OF PASS *************** *************** ************ -->
<!-- TRJ-002448 Changed to prevent the carriage from going to the secondary spittoon during printhead alignment
plots. This happens because the printmode is a mix of uni-directional and bi-directional print modes. Date 11th April 2006 -->
<begin-of-pass>
<flying-spit type="fixed">
<parameters>
<fixed mode="only-SVS"/>
</parameters>
</flying-spit>
</begin-of-pass>
<!-- *************** *************** ****** BEGIN OF PAGE *************** *************** ************ -->
<!-- Disable drop detection -->
<begin-of-page>
<drop-detection enabled="0">
</drop-detection>
</begin-of-page>
<!-- *************** *************** ****** END OF JOB *************** *************** ************ -->
<!-- Disable drop detection -->
<end-of-job>
<drop-detection enabled="0">
</drop-detection>
</end-of-job>
</servicing>
[/CODE]
keyword = 'servicing'
patt = re.compile(r'<% s(.*)>' % keyword)
s = patt.search(ope n('file_name.xm l').read()).gro up(1)
if '=' in s:
print s.split('=')[1][/code]
Output:
>>> "alignment"Comment
Comment