put XML document in json

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MD

    put XML document in json

    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


  • drclue

    #2
    Re: put XML document in json

    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


    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:{versi on:"2.0"},c:[{n:"channel",c :[{n:"title",v:"C NN.com
    -
    World"},{n:"lin k",v:"http://www.cnn.com/rssclick/WORLD/?section=cnn_wo rld"},{n:"descr iption",v:"CNN. com
    delivers up-to-the-minute news and information on the latest top
    stories, weather, entertainment, politics and
    more."},{n:"lan guage",v:"en-us"},{n:"copyri ght",v:"© 2006 Cable News
    Network LP, LLLP."},{n:"pub Date",v:"Thu, 14 Sep 2006 11:13:45
    EDT"},{n:"ttl", v:"10"},{n:"ima ge",c:[{n:"title",v:"C NN.com -
    World"},{n:"lin k",v:"http://www.cnn.com/rssclick/WORLD/?section=cnn_wo rld"},{n:"url", v:"http://i.cnn..net/cnn/.element/img/1.0/logo/cnn.logo.rss.gi f"},{n:"width", v:"144"},{n:"he ight",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:"T op Russian banker shot
    dead"},{n:"link ",v:"http://www.cnn.com/rssclick/2006/BUSINESS/09/14/russia.banker/index.html?sect ion=cnn_world"} ,{n:"descriptio n",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:"p ubDate",v:"Thu, 14 Sep 2006 03:51:31
    EDT"}]},{n:"item",c:[{n:"title",v:"A lleged gunman called himself
    &quot;Angel of
    Death&quot;"},{ n:"link",v:"htt p://www.cnn.com/rssclick/2006/WORLD/americas/09/14/montreal.shooti ng/index.html?sect ion=cnn_world"} ,{n:"descriptio n",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:"pubD ate",v:"Thu, 14 Sep 2006 10:56:10
    EDT"}]},{n:"item",c:[{n:"title",v:"M uslim bomber guilty of killing
    17"},{n:"link", v:"http://www.cnn.com/rssclick/2006/WORLD/asiapcf/09/14/india.terrortri al/index.html?sect ion=cnn_world"} ,{n:"descriptio n",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:"p ubDate",v:"Thu, 14 Sep 2006 05:59:36
    EDT"}]},{n:"item",c:[{n:"title",v:"B uilders hurt in airport roof
    fall"},{n:"link ",v:"http://www.cnn.com/rssclick/2006/WORLD/europe/09/14/spain.collapse/index.html?sect ion=cnn_world"} ,{n:"descriptio n",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:"U kraine &quot;shelve s bid to
    join
    NATO&quot;"},{n :"link",v:"http ://www.cnn.com/rssclick/2006/WORLD/europe/09/14/ukraine.nato.re ut/index.html?sect ion=cnn_world"} ,{n:"descriptio n",v:"Read
    full story for latest details."},{n:" pubDate",v:"Thu , 14 Sep 2006
    08:41:48 EDT"}]},{n:"item",c:[{n:"title",v:"D rone photo said to show
    Taliban"},{n:"l ink",v:"http://www.cnn.com/rssclick/2006/WORLD/asiapcf/09/14/afghan.drone.ap/index.html?sect ion=cnn_world"} ,{n:"descriptio n",v:"Read
    full story for latest details."},{n:" pubDate",v:"Thu , 14 Sep 2006
    03:42:30 EDT"}]},{n:"item",c:[{n:"title",v:"K enya now cocaine
    trafficking
    hub"},{n:"link" ,v:"http://www.cnn.com/rssclick/2006/WORLD/africa/09/13/kenya.coke.ap/index.html?sect ion=cnn_world"} ,{n:"descriptio n",v:"Read
    full story for latest details."},{n:" pubDate",v:"Wed , 13 Sep 2006
    23:09:47 EDT"}]},{n:"item",c:[{n:"title",v:"H ouses raided after deadly
    Turkey
    blast"},{n:"lin k",v:"http://www.cnn.com/rssclick/2006/WORLD/europe/09/13/turkey.blast/index.html?sect ion=cnn_world"} ,{n:"descriptio n",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:"pub Date",v:"Wed, 13 Sep 2006 12:09:28
    EDT"}]},{n:"item",c:[{n:"title",v:"C ollege gunman dies after
    rampage"},{n:"l ink",v:"http://www.cnn.com/rssclick/2006/WORLD/americas/09/13/montreal.shooti ng/index.html?sect ion=cnn_world"} ,{n:"descriptio n",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:"pub Date",v:"Wed, 13 Sep 2006
    22:50:24 EDT"}]},{n:"item",c:[{n:"title",v:"Z imbabwe arrests union
    leaders"},{n:"l ink",v:"http://www.cnn.com/rssclick/2006/WORLD/africa/09/13/zimbabwe.ap/index.html?sect ion=cnn_world"} ,{n:"descriptio n",v:"Read
    full story for latest details."},{n:" pubDate",v:"Wed , 13 Sep 2006
    12:41:04 EDT"}]}]}]}
    if(wFeedLoadCat ch)wFeedLoadCat ch(theXMLresult )
    if(typeof
    LAMPtransact_th robber!='undefi ned')LAMPtransa ct_throbber({co nfirmed:'XMLrel ay',cgi:'F1XMLr elay.cgi',args: 'F1_JASONcallba ck=wFeedLoadCat ch&F1_KEYRUN=41 B6767P61jLke934 9495VdOP6zQRPXb cABChinYZPPPXbc ABChinYZaoD6F7K 012j8kl9m34E5Ld eOfgMpNSTUVWqrs tuGHIJvwxy&F1_J ASON=true'});

    Comment

    • MD

      #3
      Re: put XML document in json

      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>";
      },
      ]


      Comment

      • drclue

        #4
        Re: put XML document in json

        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.

        Comment

        • Tom Cole

          #5
          Re: put XML document in json


          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.

          Comment

          • Martin Honnen

            #6
            Re: put XML document in json



            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.toJSONStrin g()
            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

            Comment

            • drclue

              #7
              Re: put XML document in json

              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.toJSONStrin g()
              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.

              Comment

              • Martin Honnen

                #8
                Re: put XML document in json



                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?num ber=4627>

                --

                Martin Honnen

                Comment

                • drclue

                  #9
                  Re: put XML document in json

                  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?num ber=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.

                  Comment

                  Working...