Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert raw data to XML

elrondrules@gmail.com
Guest
 
Posts: n/a
#1: Jan 30 '07
Hi

I am running a HTTP server which receives post from a process.
In my do_POST method am receiving raw data.

I know that this raw data has a valid XML content and I need to
convert this into an XML file.

Are there any routines to do this.. if not how to write one..

For example the raw data is as follows

<?xml version="1.0" ?><Blah><ABC><Id id="1"/><Description>SomeText </
Description><Result>PassorFail</Result></ABC></Blah>

without spaces or new lines. I need this to be written into an XML
file as

<?xml version="1.0" ?>
<Blah>
<ABC>
<Id id="1"/>
<Description>
SomeText
</Description>
<Result>
PassorFail
</Result>
</ABC>
</Blah>

The tags in the raw data are not the same alaways.. Hence I need a
generic routine that does the trick

Any pointers for this issue would help

Thanks


Justin Ezequiel
Guest
 
Posts: n/a
#2: Jan 30 '07

re: Convert raw data to XML


On Jan 30, 10:42 am, elrondru...@gmail.com wrote:
Quote:
For example the raw data is as follows
>
<?xml version="1.0" ?><Blah><ABC><Id id="1"/><Description>SomeText </
Description><Result>PassorFail</Result></ABC></Blah>
>
without spaces or new lines. I need this to be written into an XML
file as
>
<?xml version="1.0" ?>
<Blah>
<ABC>
<Id id="1"/>
<Description>
SomeText
</Description>
<Result>
PassorFail
</Result>
</ABC>
</Blah>
>
raw = r'<?xml version="1.0" ?><Blah><ABC><Id id="1"/
Quote:
><Description>SomeText </Description><Result>PassorFail</Result></
ABC></Blah>'
import xml.dom.ext
import xml.dom.minidom
doc = xml.dom.minidom.parseString(raw)
xml.dom.ext.PrettyPrint(doc)
<?xml version='1.0' encoding='UTF-8'?>
<Blah>
<ABC>
<Id id='1'/>
<Description>SomeText </Description>
<Result>PassorFail</Result>
</ABC>
</Blah>


Gabriel Genellina
Guest
 
Posts: n/a
#3: Jan 30 '07

re: Convert raw data to XML


En Mon, 29 Jan 2007 23:42:07 -0300, <elrondrules@gmail.comescribió:
Quote:
For example the raw data is as follows
>
<?xml version="1.0" ?><Blah><ABC><Id id="1"/><Description>SomeText </
Description><Result>PassorFail</Result></ABC></Blah>
>
without spaces or new lines. I need this to be written into an XML
file as
[same content but nicely indented]
Is the file supposed to be processed by humans? If not, just write it as
you receive it.
Spaces and newlines and indentation are mostly irrelevant on an xml file.

--
Gabriel Genellina

John Nagle
Guest
 
Posts: n/a
#4: Jan 30 '07

re: Convert raw data to XML


Actually, that's not "raw data" coming in, that's valid XML.
Why do you need to indent it? Just write it to a file.

If you really need to indent XML, get BeautifulSoup, read the
XML in with BeautifulStoneSoup, and write it back out with
"prettify()". But if the next thing to see that XML is a program,
not a human, why bother?

John Nagle

Justin Ezequiel wrote:
Quote:
On Jan 30, 10:42 am, elrondru...@gmail.com wrote:
>
Quote:
>>For example the raw data is as follows
>>
>><?xml version="1.0" ?><Blah><ABC><Id id="1"/><Description>SomeText </
>>Description><Result>PassorFail</Result></ABC></Blah>
>>
>>without spaces or new lines. I need this to be written into an XML
>>file as
>>
>><?xml version="1.0" ?>
>><Blah>
> <ABC>
> <Id id="1"/>
> <Description>
> SomeText
> </Description>
> <Result>
> PassorFail
> </Result>
> </ABC>
>></Blah>
>>
>
raw = r'<?xml version="1.0" ?><Blah><ABC><Id id="1"/
>
Quote:
>><Description>SomeText </Description><Result>PassorFail</Result></
>
ABC></Blah>'
import xml.dom.ext
import xml.dom.minidom
doc = xml.dom.minidom.parseString(raw)
xml.dom.ext.PrettyPrint(doc)
<?xml version='1.0' encoding='UTF-8'?>
<Blah>
<ABC>
<Id id='1'/>
<Description>SomeText </Description>
<Result>PassorFail</Result>
</ABC>
</Blah>
>
>
elrondrules@gmail.com
Guest
 
Posts: n/a
#5: Jan 30 '07

re: Convert raw data to XML


On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
Quote:
En Mon, 29 Jan 2007 23:42:07 -0300, <elrondru...@gmail.comescribió:
>
Quote:
For example the raw data is as follows
>
Quote:
<?xml version="1.0" ?><Blah><ABC><Id id="1"/><Description>SomeText </
Description><Result>PassorFail</Result></ABC></Blah>
>
Quote:
without spaces or new lines. I need this to be written into an XML
file as
[same content but nicely indented]
>
Is the file supposed to be processed by humans? If not, just write it as
you receive it.
Spaces and newlines and indentation are mostly irrelevant on an xml file.
>
--
Gabriel Genellina
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..


John Nagle
Guest
 
Posts: n/a
#6: Jan 30 '07

re: Convert raw data to XML


elrondrules@gmail.com wrote:
Quote:
On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
>
Quote:
>>En Mon, 29 Jan 2007 23:42:07 -0300, <elrondru...@gmail.comescribió:
Quote:
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
elrondrules@gmail.com
Guest
 
Posts: n/a
#7: Jan 30 '07

re: Convert raw data to XML


On Jan 30, 12:05 pm, John Nagle <n...@animats.comwrote:
Quote:
elrondru...@gmail.com wrote:
Quote:
On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
>
Quote:
Quote:
>En Mon, 29 Jan 2007 23:42:07 -0300, <elrondru...@gmail.comescribió:
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
is there any other way to do this without using BeautifulStoneSoup..
using existing minidom or ext..
i dont want to install anything new

hg
Guest
 
Posts: n/a
#8: Jan 30 '07

re: Convert raw data to XML


elrondrules@gmail.com wrote:
Quote:
On Jan 30, 12:05 pm, John Nagle <n...@animats.comwrote:
Quote:
>elrondru...@gmail.com wrote:
Quote:
On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
>>
Quote:
>>En Mon, 29 Jan 2007 23:42:07 -0300, <elrondru...@gmail.comescribió:
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
>
is there any other way to do this without using BeautifulStoneSoup..
using existing minidom or ext..
i dont want to install anything new
yes, write it ;-)




Gabriel Genellina
Guest
 
Posts: n/a
#9: Jan 30 '07

re: Convert raw data to XML


elrondrules@gmail.com wrote:
Quote:
is there any other way to do this without using BeautifulStoneSoup..
using existing minidom or ext..
i dont want to install anything new
It appears that you already know the answer... Look at the minidom
documentation, toprettyxml method.

--
Gabriel Genellina

Closed Thread