Main Tutorials

Python – Read XML file (DOM Example)

In this example, we will show you how to read an XML file and print out its values, via Python xml.dom.minidom.

1. XML File

A simple XML file, later parse it with Python minidom.

staff.xml

<?xml version="1.0"?>
<company>
	<name>Mkyong Enterprise</name>
	<staff id="1001">
		<nickname>mkyong</nickname>
		<salary>100,000</salary>
	</staff>
	<staff id="1002">
		<nickname>yflow</nickname>
		<salary>200,000</salary>
	</staff>
	<staff id="1003">
		<nickname>alex</nickname>
		<salary>20,000</salary>
	</staff>
</company>

2. DOM Example 1

A simple Python minidom example.

dom-example.py

from xml.dom import minidom

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

# doc.getElementsByTagName returns NodeList
name = doc.getElementsByTagName("name")[0]
print(name.firstChild.data)

staffs = doc.getElementsByTagName("staff")
for staff in staffs:
        sid = staff.getAttribute("id")
        nickname = staff.getElementsByTagName("nickname")[0]
        salary = staff.getElementsByTagName("salary")[0]
        print("id:%s, nickname:%s, salary:%s" %
              (sid, nickname.firstChild.data, salary.firstChild.data))

Output


Mkyong Enterprise
id:1001, nickname:mkyong, salary:100,000
id:1002, nickname:yflow, salary:200,000
id:1003, nickname:alex, salary:20,000

3. DOM Example 2

Yet another minidom example.

dom-example2.py

from xml.dom import minidom

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

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("Node Name : %s" % name.nodeName)
print("Node Value : %s \n" % getNodeText(name))


staffs = doc.getElementsByTagName("staff")
for staff in staffs:
        sid = staff.getAttribute("id")
        nickname = staff.getElementsByTagName("nickname")[0]
        salary = staff.getElementsByTagName("salary")[0]
        print("id:%s, nickname:%s, salary:%s" %
              (sid, getNodeText(nickname), getNodeText(salary)))

Output


Node Name : name
Node Value : Mkyong Enterprise 

id:1001, nickname:mkyong, salary:100,000
id:1002, nickname:yflow, salary:200,000
id:1003, nickname:alex, salary:20,000

4. Project Demo

References

  1. Python : xml.dom.minidom
  2. Python : Objects in the DOM

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Dag
6 years ago

mkyong,
thank you. Finally a library for parsing XML with a tutorial I could understand.

mak
9 years ago

am hitting “IndexError: list index out of range” error @ line 6 while executing above example1

muslim
5 years ago

I do not like your tutorial, sorry

Robin
9 years ago

Thank you for this tutorial. Very nice for starting!

Sunil
6 years ago

Hi ,
Using minidom Is is possible to print the element names?

I mean the output as below:
name
staff
nickname
Salary

DASADIYA CHAITANYA
9 years ago

Nice Post ..!!..!!!!!!!!!!!!!!