Python – Read XML file example

Posted on

XML is a tag-based language for defining structured information, commonly used to define documents and data shipped over the Web and other applications. To get information from XML, developers need to parse the XML There are different ways to parse a XML file in Python

  1. Parse XML using re Pattern Module.
  2. Parse XML using DOM parser.
  3. Parse XML using SAX parser.
  4. Parse XML using ElementTree from etree package.

Content of xml file(emp.xml) which we will be parsing using different parser in this tutorial.

1. Parse XML using re Pattern Module.

Python has inbuilt support of pattern matching which is available under re Pattern Module. One can parse xml file using pattern matching as follow.

Output

2. Parse XML using DOM parser.

DOM parser is most standard and widely used parser for XML. it parses XML text into a tree of objects and provides an interface for navigating the tree to extract tag attributes and values. Below example demonstrates parsing of XML using DOM parser.

Output

3. Parse XML using SAX parser.

Python has standard library for SAX parser. In SAX model, a class’s methods receive callbacks as a parse progresses and use state information to keep track of where they are in the document and collect its data. Below example demonstrates parsing of XML using SAX parser.

Output

4. Parse XML using ElementTree from etree package.

This method to parse XML has similar effect as DOM parse. However, lesser lines of code needs to perform similar task. the ElementTree system available in the etree package of the standard library in Python.  Below example demonstrates parsing of XML using ElementTree.

Output

Conclusion: In this tutorial, we have discussed different methods to read XML file. DOM parser and SAX parser is standard way to parse XML file and suitable for any kind of XML. While you can use pattern matching if content of xml is less and suits your application.

Download the attached zip files.

xmlparser

Stay tuned for more tutorials !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.