Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: which XML parsing library will work out-of-the-box for Python 2.4 and up?

Tags:

python

xml

How can I make sure that my Python script, which will be doing some XML parsing, will Just Work with Python 2.4, 2.5 and 2.6?

Specifically, which (if any) XML parsing library is present in, and compatible between, all those versions?

Edit: the working-out-of-the-box requirement is in place because the XML parsing I'm going to need to do is very limited (just grabbing some values) and I'm going to need to run this script on a bunch of different platforms, so I'd rather deal with a crappy XML API then try to get lxml installed on Mac, Linux and Windows.

like image 915
David Wolever Avatar asked Dec 23 '09 19:12

David Wolever


People also ask

Which module can you use to parse an XML file using Python?

Python allows parsing these XML documents using two modules namely, the xml. etree. ElementTree module and Minidom (Minimal DOM Implementation).

How do XML files work in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().

What is XML how is it used in Python explain parsing of XML with example?

Extensible Markup Language (XML) is a markup language which encodes documents by defining a set of rules in both machine-readable and human-readable format. Extended from SGML (Standard Generalized Markup Language), it lets us describe the structure of the document. In XML, we can define custom tags.


1 Answers

minidom is available in Python 2.0 and later.

However, if I were you, I would strongly consider using ElementTree which is available in Python 2.5 and later. Its syntax is much more pleasant.

2.4 users can reasonably easily download ElementTree, 2.5+ it will work without any additional dependencies. But I may be spoiled by rarely needing to target pre-2.5, myself.

like image 75
Jeffrey Harris Avatar answered Sep 23 '22 02:09

Jeffrey Harris