Open In App

Pretty Printing XML in Python

Last Updated : 21 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

XML stands for Extensible Markup Language and is used to encode a document that can be understandable by both humans and machines. Data stored in XML format is easy to understand and easy to modify. There are three main things that have to keep in mind when you are using XML – Simplicity, Generality, and Usability. 

Pretty Printing XML using xml.dom.minidom

In this approach, we will directly create XML content in our editor i.e. We will not use any external XML files.

The XML we will be using is mentioned below: –

OriginalXml = '<?xml version= "2.0" ?>
<gfg>
   <instructor>
       <Name>Sandeep Jain Sir</Name>
   </instructor>
</gfg>'

DOM (document object model) is a cross-language API from W3C i.e. World Wide Web Consortium for accessing and modifying XML documents. Python enables you to parse XML files with the help of xml.dom.minidom, which is the minimal implementation of the DOM interface. It is simpler than the full DOM API and should be considered smaller.

Python3




import xml.dom.minidom
  
OriginalXml = '<?xml version= "1.0" ?><gfg>\
<instructor><Name>Sandeep Jain Sir</Name></instructor></gfg>'
temp = xml.dom.minidom.parseString(OriginalXml)
new_xml = temp.toprettyxml()
print(new_xml)


Output:

<?xml version="1.0" ?>
<gfg>
    <instructor>
        <Name>Sandeep Jain Sir</Name>
    </instructor>
</gfg>

In this approach, we will create XML content in a file and access its content through the python open() function and the rest of the steps remain the same. For that, we need to create an XML file say gfg.xml in your code editor.

gfg.xml contains: 

<?xml version= “1.0” ?>
<gfg>
   <instructor>
<Name>Sandeep Jain Sir</Name>
   </instructor>
</gfg>

Python3




import xml.dom.minidom
  
with open('gfg.xml') as OriginalXML:
    temp = xml.dom.minidom.parseString(OriginalXML.read())
    New_XML = temp.toprettyxml()
  
print(New_XML)


Output:

<?xml version="1.0" ?>
<gfg>
    <instructor>
        <Name>Sandeep Jain Sir</Name>
    </instructor>
</gfg>

Pretty Printing XML using BeautifulSoup

In this approach, we will be using Beautiful Soup for printing the XML content.

Python3




from bs4 import BeautifulSoup
  
temp = BeautifulSoup(open("gfg.xml"), "xml")
new_xml = temp.prettify()
print(new_xml)


Output:

<?xml version="1.0" encoding="utf-8"?>
<gfg>
 <instructor>
  <Name>
   Sandeep Jain Sir
  </Name>
 </instructor>
</gfg>

Pretty Printing XML using lxml

In this method, we will be using the python lxml module.

This library converts the original XML file to a more beautiful XML file.

Python3




from lxml import etree
  
temp = etree.parse("gfg.xml")
new_xml = etree.tostring(temp, pretty_print = True, encoding = str)
print(new_xml)


Output:

<gfg>
  <instructor>
    <Name>Sandeep Jain Sir</Name>
  </instructor>
</gfg>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads