XML parsing in Python [closed] XML parsing in Python [closed] xml xml

XML parsing in Python [closed]


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

from xml.dom.minidom import parsedoc = 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.


For most of my tasks I have used the Minidom Lightweight DOM implementation, from the official page:

from xml.dom.minidom import parse, parseStringdom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by namedatasource = open('c:\\temp\\mydata.xml')dom2 = parse(datasource)   # parse an open filedom3 = parseString('<myxml>Some data<empty/> some more data</myxml>')


Here is also a very good example on how to use minidom along with explanations.