473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Convert raw data to XML

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

Jan 30 '07 #1
8 5079
On Jan 30, 10:42 am, elrondru...@gmail.com wrote:
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"/
><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>
Jan 30 '07 #2
En Mon, 29 Jan 2007 23:42:07 -0300, <el*********@gmail.comescribió:
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

Jan 30 '07 #3
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:
On Jan 30, 10:42 am, elrondru...@gmail.com wrote:
>>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"/
>><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>

Jan 30 '07 #4
On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
En Mon, 29 Jan 2007 23:42:07 -0300, <elrondru...@gmail.comescribió:
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
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..
Jan 30 '07 #5
el*********@gmail.com wrote:
On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
>>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
Jan 30 '07 #6
On Jan 30, 12:05 pm, John Nagle <n...@animats.comwrote:
elrondru...@gmail.com wrote:
On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
>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

Jan 30 '07 #7
hg
el*********@gmail.com wrote:
On Jan 30, 12:05 pm, John Nagle <n...@animats.comwrote:
>elrondru...@gmail.com wrote:
On Jan 29, 8:54 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
>>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 ;-)


Jan 30 '07 #8
el*********@gmail.com wrote:
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

Jan 30 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
5
by: simon | last post by:
I have datetime variable: Datetime tsEndTime; Should I use (DateTime): tsEndTime=(DateTime)rdr.GetValue(15) or is better to use: tsEndTime=Convert.ToDateTime(rdr.GetValue(15))
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
8
by: John A Grandy | last post by:
could someone please discuss the pros and cons of CType(MyDouble,Decimal) versus Convert.ToDecimal(MyDouble) .... and other such conversions ...
5
by: melickas | last post by:
We designed a custom application using Office Developer Tools '97 which included a Run-time version of Access--- so it would not matter if our customer even had any version of Access on their...
1
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the...
3
by: JJ | last post by:
I've got an n-layer application that fetches data from an SQL database using stored procedures. This information is read into various classes which represent a hierachical structure:. eg. ...
6
by: tshad | last post by:
Apparently, I can't do: Dim da2 As New OleDb.OleDbDataAdapter("Select PR, Convert(varchar,getchar(),1),F1, F2, F5, Sum(F4) from temp .... I am getting this error. 'undefined function...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.