Python enables you to parse and modify XML documents. In order to parse XML document, you need to have the entire XML document in memory. In this tutorial, we will see how we can use XML minidom class in Python to load and parse XML files.
How to Parse XML using minidom
We have created a sample XML file that we are going to parse. Step 1) Create Sample XML file Inside the file, we can see the first name, last name, home, and the area of expertise (SQL, Python, Testing and Business)
Step 2) Use the parse function to load and parse the XML file Once we have parsed the document, we will print out the “node name” of the root of the document and the “firstchild tagname”. Tagname and nodename are the standard properties of the XML file.
Import the xml.dom.minidom module and declare file that has to be parsed (myxml.xml) This file carries some basic information about an employee like first name, last name, home, expertise, etc. We use the parse function on the XML minidom to load and parse the XML file We have variable doc and doc gets the result of the parse function We want to print the nodename and child tagname from the file, so we declare it in print function Run the code- It prints out the nodename (#document) from the XML file and the first child tagname (employee) from the XML file
Note: Nodename and child tagname are the standard names or properties of an XML dom. Step 3) Call the list of XML tags from the XML document and printed out Next, We can also call the list of XML tags from the XML document and printed out. Here we printed out the set of skills like SQL, Python, Testing and Business.
Declare the variable expertise, from which we going to extract all the expertise name employee is having Use the dom standard function called “getElementsByTagName” This will get all the elements named skill Declare loop over each one of the skill tags Run the code- It will give list of four skills
How to Write XML Node
We can create a new attribute by using the “createElement” function and then append this new attribute or tag to the existing XML tags. We added a new tag “BigData” in our XML file.
You have to code to add the new attribute (BigData) to the existing XML tag Then, you have to print out the XML tag with new attributes appended with the existing XML tag
To add a new XML and add it to the document, we use code “doc.create elements” This code will create a new skill tag for our new attribute “Big-data” Add this skill tag into the document first child (employee) Run the code- the new tag “big data” will appear with the other list of expertise
XML Parser Example
Python 2 Example Python 3 Example
How to Parse XML using ElementTree
ElementTree is an API for manipulating XML. ElementTree is the easy way to process XML files. We are using the following XML document as the sample data: Reading XML using ElementTree: we must first import the xml.etree.ElementTree module. Now let’s fetch the root element:
output:
Summary:
Python enables you to parse the entire XML document at one go and not just one line at a time. In order to parse XML document you need to have the entire document in memory.
To parse XML document Import xml.dom.minidom Use the function “parse” to parse the document ( doc=xml.dom.minidom.parse (file name); Call the list of XML tags from the XML document using code (=doc.getElementsByTagName( “name of xml tags”) To create and add new attribute in XML document Use function “createElement”