473,396 Members | 1,758 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,396 software developers and data experts.

put XML document in json

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
8 19462
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
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
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

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


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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.