put XML document in json

Discussion in 'Javascript' started by MD, Sep 14, 2006.

  1. MD

    MD Guest

    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
     
    MD, Sep 14, 2006
    #1
    1. Advertising

  2. MD

    MD Guest

    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>";
    > },
    > ]
     
    MD, Sep 15, 2006
    #2
    1. Advertising

  3. MD

    Tom Cole Guest

    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.
     
    Tom Cole, Sep 15, 2006
    #3
  4. 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/
     
    Martin Honnen, Sep 15, 2006
    #4
  5. 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/
     
    Martin Honnen, Sep 15, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Prichard
    Replies:
    0
    Views:
    787
    Tony Prichard
    Dec 12, 2003
  2. Florian Frank
    Replies:
    0
    Views:
    264
    Florian Frank
    Jun 30, 2009
  3. sajuptpm
    Replies:
    2
    Views:
    402
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    311
    dieter
    Feb 15, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    294
    Bryan Britten
    May 28, 2013
Loading...

Share This Page