Python XML Processing

XML stands for Extensible Markup Language, is an open source language, allows you to develop the application that can be read by the other applications, regardless of OS.

Here is a simple XML file named movies.xml as an input

<collection shelf="Most Popular">
<movie title="Mission Impossible">
	<type>Action, Crime</type>
	<format>DVD</format>
	<year>2003</year>
	<rating>PG</rating>
	<stars>10</stars>
	<description>An action movie by Tom Cruise</description>
</movie>
<movie title="Rush Hour">
	<type>Action, Comedy</type>
	<format>DVD</format>
	<year>1989</year>
	<rating>R</rating>
	<stars>8</stars>
	<description>A comedy movie by Jackie Chan</description>
</movie>
	<movie title="Skyfall">
	<type>Action, Crime</type>
	<format>DVD</format>
	<episodes>4</episodes>
	<rating>PG</rating>
	<stars>10</stars>
	<description>An action movie by James Bond</description>
</movie>
<movie title="The Social Network">
	<type>Technology</type>
	<format>VHS</format>
	<rating>PG</rating>
	<stars>2</stars>
	<description>A movie based on Facebook</description>
</movie>
</collection>

Python XML Processing Example

Here is an example, demonstrating the concept and use of xml in python:

# Python XML Processing - Example Program

import xml.sax

class MovieHandler( xml.sax.ContentHandler ):
	def __init__(self):
		self.CurrentData = ""
		self.type = ""
		self.format = ""
		self.year = ""
		self.rating = ""
		self.stars = ""
		self.description = ""
	
	def startElement(self, tag, attributes):
		self.CurrentData = tag
		if tag == "movie":
			print "*****Movie*****"
			title = attributes["title"]
			print "Title:", title
	
	def endElement(self, tag):
		if self.CurrentData == "type":
			print "Type:", self.type
		elif self.CurrentData == "format":
			print "Format:", self.format
		elif self.CurrentData == "year":
			print "Year:", self.year
		elif self.CurrentData == "rating":
			print "Rating:", self.rating
		elif self.CurrentData == "stars":
			print "Stars:", self.stars
		elif self.CurrentData == "description":
			print "Description:", self.description
		self.CurrentData = ""

	def characters(self, content):
		if self.CurrentData == "type":
			self.type = content
		elif self.CurrentData == "format":
			self.format = content
		elif self.CurrentData == "year":
			self.year = content
		elif self.CurrentData == "rating":
			self.rating = content
		elif self.CurrentData == "stars":
			self.stars = content
		elif self.CurrentData == "description":
			self.description = content
  
	if( __name__ == "__main__"):
		parser = xml.sax.make_parser()
		parser.setFeature(xml.sax.handler.feature_namespaces, 0)
		Handler = MovieHandler()
		parser.setContentHandler( Handler )
		parser.parse("movies.xml")

It will produce the following result:

*****Movie*****
Title: Mission Impossible
Type: Action, Crime
Format: DVD
Year: 2003
Rating: PG
Stars: 10
Description: An action movie by Tom Cruise
*****Movie*****
Title: Rush Hour
Type: Action, Comedy
Format: DVD
Year: 1989
Rating: R
Stars: 8
Description: A comedy movie by Jackie Chan
*****Movie*****
Title: Skyfall
Type: Action, Crime
Format: DVD
Rating: PG
Stars: 10
Description: An action movie by James Bond
*****Movie*****
Title: The Social Network
Type: Technology
Format: VHS
Rating: PG
Stars: 2
Description: A movie based on Facebook

Python Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube