Is there a way to use python to extract a value from an xml file?
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.
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? -
from xml.dom.minidom import parse, parseString
-
dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name
-
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. -
<servicing name="color-measure-print-and-scan">
-
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. -
<servicing name="color-measure-print-and-scan">
-
- elements = dom1.getElementsByTagName("servicing")
-
return elements[0].attributes['name'].value
If you have multiple servicing tags, just iterate through elements instead of only looking at the first index.
My python script looks like this -
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
-
I get an error that says that return is outside function. I am using python 2.0.
My python script looks like this -
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
-
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: - import xml.dom.minidom
-
from xml.dom.minidom import parse, parseString
-
dom1 = parse('/home/test.xml')
-
elements = dom1.getElementsByTagName("servicing")
-
name = elements[0].attributes['name'].value
-
print name
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?
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.
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".
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?
Is there a way to parse the xml file using the xmllib?
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. -
<servicing name="color-measure-print-and-scan">
-
its only 1 line? can you show more sample of your xml file, if any?
sure -
<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>
-
bvdet 2,851
Expert Mod 2GB
sure -
<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>
-
If all you want to do is read that one value, you might consider a regex solution: - import re
-
-
keyword = 'servicing'
-
patt = re.compile(r'<%s(.*)>' % keyword)
-
-
s = patt.search(open('file_name.xml').read()).group(1)
-
if '=' in s:
-
print s.split('=')[1]
Output:
>>> "alignment"
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? -
for line in open("xmlfile"):
-
if line.startswith("<servicing name="):
-
line=line.replace("<servicing name=\"","").replace("\">","")
-
print line
-
using indexing/slicing: -
for line in open("xmlfile"):
-
if line.startswith("<servicing name="):
-
print line[len("<servicing name=\""):-2]
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |