471,338 Members | 1,046 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Extracting a value from an xml file

ironmonkey69
Is there a way to use python to extract a value from an xml file?
Sep 20 '07 #1
15 6483
Is there a way to use python to extract a value from an xml file?
There are many different ways to parse xml in python. I would suggest looking into a module called minidom in the python library reference. They have a simple example to help you get started.
Sep 20 '07 #2
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?
Expand|Select|Wrap|Line Numbers
  1. from xml.dom.minidom import parse, parseString
  2. dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name
  3.  
Sep 24 '07 #3
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.

Expand|Select|Wrap|Line Numbers
  1. <servicing name="color-measure-print-and-scan">
  2.  
Sep 24 '07 #4
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.

Expand|Select|Wrap|Line Numbers
  1. <servicing name="color-measure-print-and-scan">
  2.  

Expand|Select|Wrap|Line Numbers
  1. elements = dom1.getElementsByTagName("servicing")
  2. return elements[0].attributes['name'].value
If you have multiple servicing tags, just iterate through elements instead of only looking at the first index.
Sep 24 '07 #5
My python script looks like this

Expand|Select|Wrap|Line Numbers
  1. import xml.dom.minidom
  2.  
  3. from xml.dom.minidom import parse, parseString
  4. dom1 = parse('/home/test.xml') # parse an XML file by name
  5. elements = dom1.getElementsByTagName("servicing")
  6.     return elements[0].attributes['name'].value
  7.  
I get an error that says that return is outside function. I am using python 2.0.
Sep 24 '07 #6
My python script looks like this

Expand|Select|Wrap|Line Numbers
  1. import xml.dom.minidom
  2.  
  3. from xml.dom.minidom import parse, parseString
  4. dom1 = parse('/home/test.xml') # parse an XML file by name
  5. elements = dom1.getElementsByTagName("servicing")
  6.     return elements[0].attributes['name'].value
  7.  
I get an error that says that return is outside function. I am using python 2.0.
Sorry, I misunderstood what you mean by returning the value to an external program.
Try this:
Expand|Select|Wrap|Line Numbers
  1. import xml.dom.minidom
  2. from xml.dom.minidom import parse, parseString
  3. dom1 = parse('/home/test.xml')
  4. elements = dom1.getElementsByTagName("servicing")
  5. name = elements[0].attributes['name'].value
  6. print name
Sep 24 '07 #7
when i run this script, it says "ImportError: No module named xml.dom.minidom"
Is there any other way to perform this function without using minidom?
Sep 24 '07 #8
when i run this script, it says "ImportError: No module named xml.dom.minidom"
Is there any other way to perform this function without using minidom?
That'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.
Sep 24 '07 #9
bartonc
6,596 Expert 4TB
That'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.
Yep. And 2.4.4 the docs chapter is "13.7 xml.dom.minidom".
Sep 24 '07 #10
bartonc
6,596 Expert 4TB
when i run this script, it says "ImportError: No module named xml.dom.minidom"
Is there any other way to perform this function without using minidom?
This also makes no sense to me because you made it past the import in your reply #6. Something has changed since then. Can you think what that might be?
Sep 24 '07 #11
Is there a way to parse the xml file using the xmllib?
Sep 24 '07 #12
ghostdog74
511 Expert 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. <servicing name="color-measure-print-and-scan">
  2.  
its only 1 line? can you show more sample of your xml file, if any?
Sep 25 '07 #13
sure

Expand|Select|Wrap|Line Numbers
  1. <servicing name="alignment">
  2.  
  3.     <!-- ************************************  BEGIN OF PASS  ****************************************** -->
  4.  
  5.     <!-- TRJ-002448 Changed to prevent the carriage from going to the secondary spittoon during printhead alignment 
  6.         plots. This happens because the printmode is a mix of uni-directional and bi-directional print modes.  Date 11th April 2006 -->
  7.     <begin-of-pass>
  8.         <flying-spit type="fixed">                                                                                                                                                                          
  9.             <parameters>
  10.                 <fixed mode="only-SVS"/>
  11.             </parameters>
  12.         </flying-spit>
  13.     </begin-of-pass>
  14.  
  15.  
  16.     <!-- ************************************  BEGIN OF PAGE  ****************************************** -->
  17.  
  18.     <!-- Disable drop detection -->
  19.     <begin-of-page>
  20.         <drop-detection enabled="0">
  21.         </drop-detection>
  22.     </begin-of-page>
  23.  
  24.  
  25.     <!-- ************************************  END OF JOB  ****************************************** -->
  26.  
  27.     <!-- Disable drop detection -->
  28.     <end-of-job>
  29.         <drop-detection enabled="0">
  30.         </drop-detection>
  31.     </end-of-job>
  32.  
  33. </servicing>
  34.  
Sep 25 '07 #14
bvdet
2,851 Expert Mod 2GB
sure

Expand|Select|Wrap|Line Numbers
  1. <servicing name="alignment">
  2.  
  3.     <!-- ************************************  BEGIN OF PASS  ****************************************** -->
  4.  
  5.     <!-- TRJ-002448 Changed to prevent the carriage from going to the secondary spittoon during printhead alignment 
  6.         plots. This happens because the printmode is a mix of uni-directional and bi-directional print modes.  Date 11th April 2006 -->
  7.     <begin-of-pass>
  8.         <flying-spit type="fixed">                                                                                                                                                                          
  9.             <parameters>
  10.                 <fixed mode="only-SVS"/>
  11.             </parameters>
  12.         </flying-spit>
  13.     </begin-of-pass>
  14.  
  15.  
  16.     <!-- ************************************  BEGIN OF PAGE  ****************************************** -->
  17.  
  18.     <!-- Disable drop detection -->
  19.     <begin-of-page>
  20.         <drop-detection enabled="0">
  21.         </drop-detection>
  22.     </begin-of-page>
  23.  
  24.  
  25.     <!-- ************************************  END OF JOB  ****************************************** -->
  26.  
  27.     <!-- Disable drop detection -->
  28.     <end-of-job>
  29.         <drop-detection enabled="0">
  30.         </drop-detection>
  31.     </end-of-job>
  32.  
  33. </servicing>
  34.  
If all you want to do is read that one value, you might consider a regex solution:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. keyword = 'servicing'
  4. patt = re.compile(r'<%s(.*)>' % keyword)
  5.  
  6. s = patt.search(open('file_name.xml').read()).group(1)
  7. if '=' in s:
  8.     print s.split('=')[1]
Output:
>>> "alignment"
Sep 25 '07 #15
ghostdog74
511 Expert 256MB
looks like you only have one line of "<servicing name" in your xml file. so what's stopping you for using string functions like replace, or even indexing?
Expand|Select|Wrap|Line Numbers
  1. for line in open("xmlfile"):
  2.     if line.startswith("<servicing name="):
  3.         line=line.replace("<servicing name=\"","").replace("\">","")
  4.         print line
  5.  
using indexing/slicing:
Expand|Select|Wrap|Line Numbers
  1. for line in open("xmlfile"):
  2.     if line.startswith("<servicing name="):
  3.         print line[len("<servicing name=\""):-2]
  4.  
Sep 26 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Nazgul | last post: by
2 posts views Thread by Steve | last post: by
5 posts views Thread by Michael Hill | last post: by
1 post views Thread by Cognizance | last post: by
4 posts views Thread by dexter48 | last post: by
1 post views Thread by TYR | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.