Convert raw data to XML

John Nagle nagle at animats.com
Tue Jan 30 21:05:54 CET 2007


elrondrules at gmail.com wrote:
> On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl... at yahoo.com.ar> wrote:
> 
>>En Mon, 29 Jan 2007 23:42:07 -0300, <elrondru... at gmail.com> escribió:

> the reason I wanted to write it as a file was to parse the file, look 
> for a specific attribute and execute a set of commands based on the 
> value of the attribute.. also i needed to display the output of the 
> http post in a more readable format..

    That's straightforward.  You confused people by asking the
wrong question.  You wrote "Convert raw data to XML", but what
you want to do is parse XML and extract data from it.

    This will do what you want:

	http://www.crummy.com/software/BeautifulSoup/

For starters, try

	from BeautifulSoup import BeautifulStoneSoup
	xmlstring = somexml 	## get your XML into here as one big string
	soup = BeautifulStoneSoup(xmlstring)	# parse XML into tree
	print soup.prettify()	# print out in indented format

"soup" is a tree structure representing the XML, and there are
functions to easily find items in the tree by tag name, attribute,
and such.  Work on the tree, not a file with the text of the indented
output.


				John Nagle



More information about the Python-list mailing list