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″?>
<response>
<categoryHistogram>
<categoryID>image.vary.jpg</categoryID>

<count>10</count>

</category>

</categoryHistogram>

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

<item>

<itemID>414</itemID>

<url>http://—-.com/ws/web/414_1_0_1.jpg</url&gt;

<similarity>0.55</similarity>

</item>

</items>
</response>”’

import StringIO
import urllib2
import lxml.etree as ET

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

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

Leave a comment