Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

2/6/2021 Parsing XML with DOM APIs in Python - GeeksforGeeks

Related Articles

Parsing XML with DOM APIs in Python


Last Updated : 10 May, 2020

The Document Object Model (DOM) is a programming inter face 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:

<?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,

from xml.dom import minidom

doc = minidom.parse("sample.xml")

# doc.getElementsByTagName
We returns
use cookies to ensure you have the best browsing the NodeList
experience on our website. By using our site, you acknowledge
name = doc.getElementsByTagName("name")[0]
that you have read and understood our Cookie Policy & Privacy Policy
print(name.firstChild.data)
Got It !

https://www.geeksforgeeks.org/parsing-xml-with-dom-apis-in-python/?ref=rp 1/5
2/6/2021 Parsing XML with DOM APIs in Python - GeeksforGeeks

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:

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)))
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Output :
Got It !

https://www.geeksforgeeks.org/parsing-xml-with-dom-apis-in-python/?ref=rp 2/5
2/6/2021 Parsing XML with DOM APIs in Python - GeeksforGeeks

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

Attention geek! Strengthen your foundations with the P ython Programming

Foundation Course and learn the basic s.

To begin with, your inter view preparations Enhance your Data Structures concepts

with the P ython DS Course.

 Like 0

 Previous Next 

RECOMMENDED ARTICLES Page : 1 2 3

XML parsing in Python Argparse VS Docopt VS Click -


01 05
13, Jan 17
Comparing Python Command-
Line Parsing Libraries
Parsing tables and XML with 01, Jun 20

02
BeautifulSoup
25, Nov 20
Parsing PDFs in Python with Tika
06 14, Aug 20

Exposing ML/DL Models as REST


We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
03
APIs that you have read and understood our Cookie Policy & Privacy Policy
16, Jan 19
Parsing and Processing URL using
Got It07! Python - Regex
31, Aug 20

https://www.geeksforgeeks.org/parsing-xml-with-dom-apis-in-python/?ref=rp 3/5
2/6/2021 Parsing XML with DOM APIs in Python - GeeksforGeeks
g

Command-Line Option and


04 Argument Parsing using argparse
in Python NLP | Partial parsing with Regex
08 22, Feb 19
07, Feb 20

Ar ticle Contributed By :

RajuKumar19
@RajuKumar19

Vote for di culty

Easy Normal Medium Hard Expert

Article Tags : Python-XML , Python

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

5th Floor, A-118,


We use cookies to ensure you have theSector-136,
best browsingNoida, Uttar
experience on Pradesh - 201305
our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
feedback@geeksforgeeks.org
Got It !

https://www.geeksforgeeks.org/parsing-xml-with-dom-apis-in-python/?ref=rp 4/5
2/6/2021 Parsing XML with DOM APIs in Python - GeeksforGeeks

Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects

Copyright Policy Video Tutorials

Practice Contribute
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks , Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy

Got It !

https://www.geeksforgeeks.org/parsing-xml-with-dom-apis-in-python/?ref=rp 5/5

You might also like