Parsing XML with DOM APIs in Python Last Updated : 10 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The Document Object Model (DOM) is a programming interface for HTML and XML(Extensible markup language) documents. It defines the logical structure of documents and the way a document is accessed and manipulated. Parsing XML with DOM APIs in python is pretty simple. For the purpose of example we will create a sample XML document (sample.xml) as below: html <?xml version="1.0"?> <company> <name>GeeksForGeeks Company</name> <staff id="1"> <name>Amar Pandey</name> <salary>8.5 LPA</salary> </staff> <staff id="2"> <name>Akbhar Khan</name> <salary>6.5 LPA</salary> </staff> <staff id="3"> <name>Anthony Walter</name> <salary>3.2 LPA</salary> </staff> </company> Now, let's parse the above XML using python. The below code demonstrates the process, Python3 1== from xml.dom import minidom doc = minidom.parse("sample.xml") # doc.getElementsByTagName returns the NodeList name = doc.getElementsByTagName("name")[0] print(name.firstChild.data) staffs = doc.getElementsByTagName("staff") for staff in staffs: staff_id = staff.getAttribute("id") name = staff.getElementsByTagName("name")[0] salary = staff.getElementsByTagName("salary")[0] print("id:% s, name:% s, salary:% s" % (staff_id, name.firstChild.data, salary.firstChild.data)) Output: GeeksForGeeks Company id:1, name: Amar Pandey, salary:8.5 LPA id:2, name: Akbar Khan, salary:6.5 LPA id:3, name: Anthony Walter, salary:3.2 LPA The same can also be done using a user-defined function as shown in the code below: Python3 1== from xml.dom import minidom doc = minidom.parse("sample.xml") # user-defined function def getNodeText(node): nodelist = node.childNodes result = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: result.append(node.data) return ''.join(result) name = doc.getElementsByTagName("name")[0] print("Company Name : % s \n" % getNodeText(name)) staffs = doc.getElementsByTagName("staff") for staff in staffs: staff_id = staff.getAttribute("id") name = staff.getElementsByTagName("name")[0] salary = staff.getElementsByTagName("salary")[0] print("id:% s, name:% s, salary:% s" % (staff_id, getNodeText(name), getNodeText(salary))) Output: Company Name : GeeksForGeeks Company id:1, name:Amar Pandey, salary:8.5 LPA id:2, name:Akbhar Khan, salary:6.5 LPA id:3, name:Anthony Walter, salary:3.2 LPA Comment More infoAdvertise with us Next Article How to Parse and Modify XML in Python? R RajuKumar19 Follow Improve Article Tags : Python Python-XML Practice Tags : python Similar Reads XML parsing in Python This article focuses on how one can parse a given XML file and extract some useful data out of it in a structured way. XML: XML stands for eXtensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable.That's why, the design goals of X 7 min read Parse XML using Minidom in Python 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 1 min read Modify XML files with Python Python|Modifying/Parsing XMLÂ Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.The design goals of XML focus on simplicity, generality, and usability across the Internet.It is a textua 4 min read How to Parse and Modify XML in Python? XML stands for Extensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable. Thatâs why, the design goals of XML emphasize simplicity, generality, and usability across the Internet. Note: For more information, refer to XML | Basics H 4 min read Turning a Dictionary into XML in Python XML stands for Extensible Markup Language. XML was designed to be self-descriptive and to store and transport data. XML tags are used to identify, store and organize the data. The basic building block of an XML document is defined by tags. An element has a beginning tag and an ending tag. All elemen 3 min read What is Parsel in Python? Parsel is a library of Python which is designed for extracting and processing data from HTML and XML documents. It is widely used for web scraping and data extraction. It provides a simple and intuitive API for querying and parsing web content. It supports both XPath and CSS selectors to make it a v 4 min read Like