What is XML?

XML stands for eXtensible Markup Language. It was designed to store and transport minor to medium amounts of information and is widely used for sharing structured information.

Python enables y'all to parse and modify XML document. In society to parse XML document you need to have the unabridged XML document in retention. In this tutorial, nosotros will see how we can use XML minidom class in Python to load and parse XML file.

In this tutorial, nosotros will learn-

  • How to Parse XML using minidom
  • How to Create XML Node
  • How to Parse XML using ElementTree

How to Parse XML using minidom

We have created a sample XML file that nosotros are going to parse.

Step one) Inside file, we can meet showtime name, last name, home and the surface area of expertise (SQL, Python, Testing and Business)

Python XML Parser Tutorial: Create & Read XML with Examples

Step 2) Once nosotros have parsed the certificate, we will print out the "node name" of the root of the document and the "firstchild tagname". Tagname and nodename are the standard backdrop of the XML file.

Python XML Parser Tutorial: Create & Read XML with Examples

  • Import the xml.dom.minidom module and declare file that has to be parsed (myxml.xml)
  • This file carries some basic information about employee like first proper noun, last name, home, expertise, etc.
  • We use the parse part on the XML minidom to load and parse the XML file
  • We have variable doctor and doc gets the issue of the parse function
  • We want to print the nodename and child tagname from the file, and so we declare it in print function
  • Run the code- Information technology 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. In case if y'all are not familiar with these type of naming conventions.

Step three) Nosotros can as well call the list of XML tags from the XML certificate and printed out. Hither we printed out the set of skills like SQL, Python, Testing and Business.

Python XML Parser Tutorial: Create & Read XML with Examples

  • 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 lawmaking- It will give list of 4 skills

How to Create XML Node

We can create a new aspect past using "createElement" office so suspend this new aspect or tag to the existing XML tags. We added a new tag "BigData" in our XML file.

  1. You lot have to code to add the new attribute (BigData) to the existing XML tag
  2. So yous have to print out the XML tag with new attributes appended with existing XML tag

Python XML Parser Tutorial: Create & Read XML with Examples

  • To add a new XML and add information technology to the certificate, we apply code "doctor.create elements"
  • This code will create a new skill tag for our new aspect "Big-information"
  • Add together this skill tag into the certificate first child (employee)
  • Run the code- the new tag "large data" will announced with the other list of expertise

XML Parser Example

Python two Case

import xml.dom.minidom  def primary(): # apply the parse() function to load and parse an XML file    physician = xml.dom.minidom.parse("Myxml.xml");    # print out the document node and the proper noun of the first child tag    print dr..nodeName    print md.firstChild.tagName    # get a list of XML tags from the document and print each one    expertise = dr..getElementsByTagName("expertise")    print "%d expertise:" % expertise.length    for skill in expertise:      print skill.getAttribute("name")      # create a new XML tag and add it into the document    newexpertise = medico.createElement("expertise")    newexpertise.setAttribute("name", "BigData")    dr..firstChild.appendChild(newexpertise)    print " "     expertise = doc.getElementsByTagName("expertise")    impress "%d expertise:" % expertise.length    for skill in expertise:      print skill.getAttribute("name")      if name == "__main__":   main();

Python iii Example

import xml.dom.minidom  def main():     # apply the parse() function to load and parse an XML file     doc = xml.dom.minidom.parse("Myxml.xml");      # print out the document node and the proper name of the commencement child tag     print (doc.nodeName)     print (doctor.firstChild.tagName)     # go a list of XML tags from the certificate and impress each one     expertise = doc.getElementsByTagName("expertise")     print ("%d expertise:" % expertise.length)     for skill in expertise:         print (skill.getAttribute("name"))      # create a new XML tag and add information technology into the document     newexpertise = doc.createElement("expertise")     newexpertise.setAttribute("name", "BigData")     doc.firstChild.appendChild(newexpertise)     impress (" ")      expertise = doc.getElementsByTagName("expertise")     print ("%d expertise:" % expertise.length)     for skill in expertise:         print (skill.getAttribute("proper noun"))  if __name__ == "__main__":     master();

How to Parse XML using ElementTree

ElementTree is an API for manipulating XML. ElementTree is the like shooting fish in a barrel way to process XML files.

Nosotros are using the following XML document as the sample data:

<data>    <items>       <item name="expertise1">SQL</item>       <particular proper name="expertise2">Python</item>    </items> </information>                

Reading XML using ElementTree:

nosotros must offset import the xml.etree.ElementTree module.

import xml.etree.ElementTree equally ET                

Now let'due south fetch the root element:

root = tree.getroot()                

Following is the complete code for reading above xml data

import xml.etree.ElementTree as ET tree = ET.parse('items.xml') root = tree.getroot()  # all items information print('Expertise Data:')  for elem in root:    for subelem in elem:       print(subelem.text)                

output:

Expertise Data: SQL Python                

Summary:

Python enables you lot to parse the entire XML document at 1 go and not just one line at a time. In order to parse XML document you need to take the entire document in retentiveness.

  • To parse XML document
  • Import xml.dom.minidom
  • Use the function "parse" to parse the document ( physician=xml.dom.minidom.parse (file name);
  • Call the listing of XML tags from the XML document using code (=physician.getElementsByTagName( "name of xml tags")
  • To create and add new attribute in XML certificate
  • Utilise function "createElement"