By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

put XML document in json

P: n/a
MD
I want to put the xml document into the JSON like the following.

[
{
"id" : "123333",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
{
"id" : "123334",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
]

However, usually my xml document has new line for the xml document. Is there
any option to skip the new line in the JSON or I just have to remove them
manually .
MD
Sep 14 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
MD wrote:
I want to put the xml document into the JSON like the following.

[
{
"id" : "123333",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
{
"id" : "123334",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
]

However, usually my xml document has new line for the xml document. Is there
any option to skip the new line in the JSON or I just have to remove them
manually .

Well, I turn all my server side outputs into JASONized XML
and turn them back into XML after I receive them in the browser.

Below is an example ( albeit a bulky one ) from one of my
server side proxies as a result of fetching a CNN news feed
http://rss.cnn.com/rss/cnn_world.rss

It provides me with both the JASONized XML from the RSS feed
and two callback functions. One for the javascript widget that
initiated the request and another for the connection manger
that retries failed/timed out requests.
theXMLresult={n:"rss",a:{version:"2.0"},c:[{n:"channel",c:[{n:"title",v:"CNN.com
-
World"},{n:"link",v:"http://www.cnn.com/rssclick/WORLD/?section=cnn_world"},{n:"description",v:"CNN.com
delivers up-to-the-minute news and information on the latest top
stories, weather, entertainment, politics and
more."},{n:"language",v:"en-us"},{n:"copyright",v:"© 2006 Cable News
Network LP, LLLP."},{n:"pubDate",v:"Thu, 14 Sep 2006 11:13:45
EDT"},{n:"ttl",v:"10"},{n:"image",c:[{n:"title",v:"CNN.com -
World"},{n:"link",v:"http://www.cnn.com/rssclick/WORLD/?section=cnn_world"},{n:"url",v:"http://i.cnn..net/cnn/.element/img/1.0/logo/cnn.logo.rss.gif"},{n:"width",v:"144"},{n:"height" ,v:"33"},{n:"description",v:"CNN.com
delivers up-to-the-minute news and information on the latest top
stories, weather, entertainment, politics and
more."}]},{n:"item",c:[{n:"title",v:"Top Russian banker shot
dead"},{n:"link",v:"http://www.cnn.com/rssclick/2006/BUSINESS/09/14/russia.banker/index.html?section=cnn_world"},{n:"description",v: "The
top deputy chairman of Russia&quot;s Central Bank died Thursday morning
after being gunned down by two assailants the night before near a Moscow
football stadium, a medical source told Russia&quot;s state-run Interfax
news agency."},{n:"pubDate",v:"Thu, 14 Sep 2006 03:51:31
EDT"}]},{n:"item",c:[{n:"title",v:"Alleged gunman called himself
&quot;Angel of
Death&quot;"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/americas/09/14/montreal.shooting/index.html?section=cnn_world"},{n:"description",v: "The
alleged gunman in a Montreal college shooting called himself the
&quot;Angel of Death&quot; in an online profile, Canadian media
say."},{n:"pubDate",v:"Thu, 14 Sep 2006 10:56:10
EDT"}]},{n:"item",c:[{n:"title",v:"Muslim bomber guilty of killing
17"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/asiapcf/09/14/india.terrortrial/index.html?section=cnn_world"},{n:"description",v: "An
Indian court found Mohammed Ghansar guilty of murder Thursday for his
role in a string of bombings that blasted through the country&quot;s
commercial capital, Mumbai, 13 years ago, killing 257
people."},{n:"pubDate",v:"Thu, 14 Sep 2006 05:59:36
EDT"}]},{n:"item",c:[{n:"title",v:"Builders hurt in airport roof
fall"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/europe/09/14/spain.collapse/index.html?section=cnn_world"},{n:"description",v: "Twenty
people were trapped for a time Thursday at Spain&quot;s Menorca airport
when a roof collapsed at an airport, according to CNN+ quoting the
Balearics regional government."},{n:"pubDate",v:"Thu, 14 Sep 2006
07:53:43 EDT"}]},{n:"item",c:[{n:"title",v:"Ukraine &quot;shelves bid to
join
NATO&quot;"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/europe/09/14/ukraine.nato.reut/index.html?section=cnn_world"},{n:"description",v: "Read
full story for latest details."},{n:"pubDate",v:"Thu, 14 Sep 2006
08:41:48 EDT"}]},{n:"item",c:[{n:"title",v:"Drone photo said to show
Taliban"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/asiapcf/09/14/afghan.drone.ap/index.html?section=cnn_world"},{n:"description",v: "Read
full story for latest details."},{n:"pubDate",v:"Thu, 14 Sep 2006
03:42:30 EDT"}]},{n:"item",c:[{n:"title",v:"Kenya now cocaine
trafficking
hub"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/africa/09/13/kenya.coke.ap/index.html?section=cnn_world"},{n:"description",v: "Read
full story for latest details."},{n:"pubDate",v:"Wed, 13 Sep 2006
23:09:47 EDT"}]},{n:"item",c:[{n:"title",v:"Houses raided after deadly
Turkey
blast"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/europe/09/13/turkey.blast/index.html?section=cnn_world"},{n:"description",v: "Police
have raided houses in the southeastern Turkish city of Diyarbakir,
according to reports, after a bomb blast Tuesday evening killed 11
people -- many of them children -- and wounded at least a dozen
more."},{n:"pubDate",v:"Wed, 13 Sep 2006 12:09:28
EDT"}]},{n:"item",c:[{n:"title",v:"College gunman dies after
rampage"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/americas/09/13/montreal.shooting/index.html?section=cnn_world"},{n:"description",v: "A
gunman was killed by police and 20 students wounded after a shooting at
a college in Montreal, police said. Initial reports indicated up to four
people were killed, but Yvan Delorme, chief of Montreal Police Services,
clarified the number of dead."},{n:"pubDate",v:"Wed, 13 Sep 2006
22:50:24 EDT"}]},{n:"item",c:[{n:"title",v:"Zimbabwe arrests union
leaders"},{n:"link",v:"http://www.cnn.com/rssclick/2006/WORLD/africa/09/13/zimbabwe.ap/index.html?section=cnn_world"},{n:"description",v: "Read
full story for latest details."},{n:"pubDate",v:"Wed, 13 Sep 2006
12:41:04 EDT"}]}]}]}
if(wFeedLoadCatch)wFeedLoadCatch(theXMLresult)
if(typeof
LAMPtransact_throbber!='undefined')LAMPtransact_th robber({confirmed:'XMLrelay',cgi:'F1XMLrelay.cgi', args:'F1_JASONcallback=wFeedLoadCatch&F1_KEYRUN=41 B6767P61jLke9349495VdOP6zQRPXbcABChinYZPPPXbcABChi nYZaoD6F7K012j8kl9m34E5LdeOfgMpNSTUVWqrstuGHIJvwxy &F1_JASON=true'});
Sep 14 '06 #2

P: n/a
MD
Thanks for reply,

However, I don't want to conver to any format but json.
We have xml document for each record in oracle xml type,
I just want to get it and pass to the browser from servlet as a search
result.

[
{
"id" : "123333",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
{
"id" : "123334",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
]


Sep 14 '06 #3

P: n/a
MD wrote:
Thanks for reply,

However, I don't want to conver to any format but json.
We have xml document for each record in oracle xml type,
I just want to get it and pass to the browser from servlet as a search
result.

[
> {
"id" : "123333",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
{
"id" : "123334",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
]
Both your sample here and the one I gave are JASON.
Mine is just all squeezed on one line, and has a couple of
javascript calls tacked on the end.

The other thing that differs between our approaches
is that on the browser side , a single call to
my JASON2XML() function turns my JASON result sets
no matter the query back into XML that can be used with XSLT and
other facilities in the browser.

The orginal basic question you had I believe was about
newlines. Answer: ( don't use them )

The big sample result was just so you could see a live example
and notice not only that is was devoid of newlines, but
possibly answer other questions you might come across by having a live
sample to reference.
Sep 14 '06 #4

P: n/a

drclue wrote:
MD wrote:
Thanks for reply,

However, I don't want to conver to any format but json.
We have xml document for each record in oracle xml type,
I just want to get it and pass to the browser from servlet as a search
result.

[
{
"id" : "123333",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
{
"id" : "123334",
"xmldoc": "<? xml version=\"1.0\" ?><data><link></link></data>";
},
]

Both your sample here and the one I gave are JASON.
Just for future reference, it's JSON (I went to school with a Jason,
though). Wouldn't want you to put JASON on a job application or
anything, might loose some "cred" with the employer...

I once had a friend ask me if I'd heard anything about some new
technology called Rudy on Rails. I snickered (inside of course).
Mine is just all squeezed on one line, and has a couple of
javascript calls tacked on the end.

The other thing that differs between our approaches
is that on the browser side , a single call to
my JASON2XML() function turns my JASON result sets
no matter the query back into XML that can be used with XSLT and
other facilities in the browser.

The orginal basic question you had I believe was about
newlines. Answer: ( don't use them )

The big sample result was just so you could see a live example
and notice not only that is was devoid of newlines, but
possibly answer other questions you might come across by having a live
sample to reference.
Sep 15 '06 #5

P: n/a


MD wrote:

However, usually my xml document has new line for the xml document. Is there
any option to skip the new line in the JSON or I just have to remove them
manually .
I am not sure I understand the problem, if you build JSON with
<http://www.json.org/json.js>
then the result you get for e.g.
var s = ['<gods>', '<god>Kibo</god>', '</gods>'].join('\r\n');
var obj = { xml: s };
obj.toJSONString()
is
{"xml":"<gods>\r\n<god>Kibo</god>\r\n</gods>"}
so all line break/carriage return characters are properly escaped.
Simply use that library or the proper library for other languages (e.g.
PHP) to create your JSON data and line breaks/carriage returns in
strings (of XML or otherwise) should not be a problem, they will simply
be escaped as needed.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 15 '06 #6

P: n/a
Martin Honnen wrote:
MD wrote:
>However, usually my xml document has new line for the xml document. Is
there
any option to skip the new line in the JSON or I just have to remove them
manually .

I am not sure I understand the problem, if you build JSON with
<http://www.json.org/json.js>
then the result you get for e.g.
var s = ['<gods>', '<god>Kibo</god>', '</gods>'].join('\r\n');
var obj = { xml: s };
obj.toJSONString()
is
{"xml":"<gods>\r\n<god>Kibo</god>\r\n</gods>"}
so all line break/carriage return characters are properly escaped.
Simply use that library or the proper library for other languages (e.g.
PHP) to create your JSON data and line breaks/carriage returns in
strings (of XML or otherwise) should not be a problem, they will simply
be escaped as needed.
I think the OP's newline issue might actually be related not so much to
the data itself , but rather spreading the JSON syntax over multiple
lines of source which can cause parsing issues.
Sep 15 '06 #7

P: n/a


drclue wrote:

I think the OP's newline issue might actually be related not so much to
the data itself , but rather spreading the JSON syntax over multiple
lines of source which can cause parsing issues.
I think JSON allows white space like line breaks between
tokens/delimiters: <http://www.ietf.org/rfc/rfc4627.txt?number=4627>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 15 '06 #8

P: n/a
Martin Honnen wrote:
drclue wrote:
>I think the OP's newline issue might actually be related not so much
to the data itself , but rather spreading the JSON syntax over
multiple lines of source which can cause parsing issues.

I think JSON allows white space like line breaks between
tokens/delimiters: <http://www.ietf.org/rfc/rfc4627.txt?number=4627>
While I'm all for the specs , the various parsers of the
various content types over the years don't always share
my appreciation for same, so over time I just code along
a path that makes as many of them happy as possible.

In this instance , no newlines.
Sep 15 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.