Home > Python > Parsing xml using Python

Parsing xml using Python

February 5, 2012 Leave a comment Go to comments

Just a simple example of parsing the xml file using python.

Suppose your xml files like this:

”'<?xml version=”1.0″?>




<items categoryID=”image.vary.jpg”>







import StringIO
import urllib2
import lxml.etree as ET

for _, element in ET.iterparse(xml_file, tag='category'):
	 print('%s -- %s' % (element.findtext('categoryID'), element.findtext('count')))
for _, element in ET.iterparse(xml_file, tag='item'):
	 print('%s -- %s -- %s' % (element.findtext('itemID'), element.findtext('url'), element.findtext('similairty')))

Categories: Python Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: