Python lang. XML parsing using xml.dom.minidom and xml.parsers.expat


You can parse XML documents using Python language with the following code piece;

XML Data Content;

<?xml version="1.0" encoding="UTF-8"?>
<SERVERCONFIG>
<SERVERS>
<SERVER NAME="server1" ENABLED="Y">
<IP>10.1.2.1</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>12345</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server2" ENABLED="Y">
<IP>10.1.2.2</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>54321</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server3" ENABLED="N">
<IP>10.1.2.3</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>012345</PASSWORD>
<DESCRIPTION>Application Server3</DESCRIPTION>
</SERVER>
<SERVER NAME="server4" ENABLED="Y">
<IP>10.1.2.4</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>0123456</PASSWORD>
<DESCRIPTION>Application Server4</DESCRIPTION>
</SERVER>
<SERVER NAME="server5" ENABLED="N">
<IP>10.1.2.5</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>01234567</PASSWORD>
<DESCRIPTION>Application Server5</DESCRIPTION>
</SERVER>
</SERVERS>
</SERVERCONFIG>

XMLParsing.py


#Created on Jul 24, 2013

#@author: tuna

#XML Parsing example
from xml.dom.minidom import parse, parseString
import xml.parsers.expat

#loading XML to variable
XML = parse('C://config.xml')

domRunTimeList = parseString('<xmlRunTime><data>hi xml</data></xmlRunTime>')

#format XML runTime
print (domRunTimeList.toprettyxml("\t", "\n", None))

print ("\n") #new line

print ("ENABLED SERVER LIST\n")
for node in XML.getElementsByTagName('SERVER'):
 if node.getAttribute("ENABLED")=="Y":
 print ("IP --> ",str(node.getElementsByTagName("IP")[0].firstChild.nodeValue) , "NAME", node.getAttribute("NAME"))

print ("\n") #new line
print ("all IP LISTING STARTS\n")
for node in XML.getElementsByTagName('SERVER'):
 print ("IP --> ",str(node.getElementsByTagName("IP")[0].firstChild.nodeValue))

print ("\n") #new line

#XML parsing with on the fly events

# Handlers
def start(name, attrs):
 print ('Start Element:', name, attrs)
def end(name):
 print ('End Element:', name)
def text(txt):
 print ('Text:', str(txt))

#create a parser
parser = xml.parsers.expat.ParserCreate()

#map related handlers events
parser.StartElementHandler = start
parser.EndElementHandler = end
parser.CharacterDataHandler = text

parser.Parse("<xmlRunTime><data att='this is attribute1' att2='This is attribute 2'>hi xml</data></xmlRunTime>");

output;

<?xml version=”1.0″ ?>
<xmlRunTime>
<data>hi xml</data>
</xmlRunTime>

ENABLED SERVER LIST

IP –> 10.1.2.1 NAME server1
IP –> 10.1.2.2 NAME server2
IP –> 10.1.2.4 NAME server4
all IP LISTING STARTS

IP –> 10.1.2.1
IP –> 10.1.2.2
IP –> 10.1.2.3
IP –> 10.1.2.4
IP –> 10.1.2.5
Start Element: xmlRunTime {}
Start Element: data {‘att’: ‘this is attribute1’, ‘att2’: ‘This is attribute 2’}
Text: hi xml
End Element: data
End Element: xmlRunTime

Advertisements

One thought on “Python lang. XML parsing using xml.dom.minidom and xml.parsers.expat

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s