Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

how to convert XML into a JSONObject

2007-01-30
2013-04-18
  • Lock & Lock
    Lock & Lock
    2007-01-30

    great project! I'm working with JSON, I need to convert XML into a JSONObject. Can you please, provide me with the sample code? Will that be a persistent JSONObject?

     
    • chaidb-admin
      chaidb-admin
      2007-01-30

      You should use XML.toJSONPObject method to convert the XML into an in-memory JSONObject. Then use one of the methods of JSONFactory class to persist this object. For example:

      JSONObject inMemory = XML.toJSONObject("<xml one = 1 two=' \&quot;2\&quot; '><five></five>First \u0009&lt;content&gt;<five></five> This is \&quot;content\&quot;. <three>  3  </three>JSON does not preserve the sequencing of elements and contents.<three>  III  </three>  <three>  T H R E E</three><four/>Content text is an implied structure in XML. <six content=\&quot;6\&quot;/>JSON does not have implied structure:<seven>7</seven>everything is explicit.<![CDATA[CDATA blocks<are><supported>!]]></xml>");
      JSONObject persistentOne = factory.getPersistentJSONObject("xmltest", inMemory.toString());