473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 6666
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

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

Similar topics

5
by: Nazgul | last post by:
Hi! I want to implement a small tool in Python for distributing "patches" and I need Your advice. This application should be able to package all files chosen by a user into a self-extracting.exe...
2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
2
by: Avi | last post by:
hi, Can anyone tell me what the problem is and how to solve it The following piece of code resides on an asp page on the server and is used to download files from the server to the machine...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
1
by: Cognizance | last post by:
Hi gang, I'm an ASP developer by trade, but I've had to create client side scripts with JavaScript many times in the past. Simple things, like validating form elements and such. Now I've been...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
0
by: Sunil Basu | last post by:
Hi, I have a interesting thing to know and discuss with you. I am extracting data from an Excel file in a Delphi DbGrid through SQL. I want to create a criteria on a specific cell value of the...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
1
by: TYR | last post by:
I have a large dump file that originated in a MySQL db; I need to get it into an SQLite file. Various options are suggested around the web; none of them seem to work (most failing to import the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.