XML parsing in Python

Alex picture Alex · Sep 3, 2009 · Viewed 10.7k times · Source

I'd like to parse a simple, small XML file using python however work on pyXML seems to have ceased. I'd like to use python 2.6 if possible. Can anyone recommend an XML parser that will work with 2.6?

Thanks

Answer

Eli Courtwright picture Eli Courtwright · Sep 3, 2009

If it's small and simple then just use the standard library:

from xml.dom.minidom import parse
doc = parse("filename.xml")

This will return a DOM tree implementing the standard Document Object Model API

If you later need to do complex things like schema validation or XPath querying then I recommend the third-party lxml module, which is a wrapper around the popular libxml2 C library.