How to convert XML to JSON by using JavaScript or Java?

<p>&nbsp;</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">Over the last years the simple data format JSON had a great inpetus in the service-oriented architecture and especially in the area of internet services. It is widely used because of its compactness and easy understandability.</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">&nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">JSON is lightweight and was used for several of my projects. However, the convertion by hand (Or by an own routine) is an unacceptable quality problem.</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">&nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">A transformation could look like the this example:</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">&nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">The&nbsp;<strong>XML </strong>of image xml.png&nbsp;<span style="font-family: 'Trebuchet MS', sans-serif; font-size: 13px;">should be transformed to the&nbsp;<strong>JSON</strong> representation of image json.png.</span></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px 'Trebuchet MS'; min-height: 15.0px;">So to a simple representation using curly braces.</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; line-height: 21.0px; font: 13.0px 'Trebuchet MS';"><span style="font-family: Helvetica, sans-serif; font-size: small;"><span style="font-size: 12px;"><span style="font-family: 'Trebuchet MS', sans-serif; font-size: small;"><span style="font-size: 13px;"><br /></span></span></span></span></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; line-height: 21.0px; font: 12.0px Helvetica;">JavaScript could be used to convert the data before it is passed on to the Java application. The conversion could also be done in Java itself.</p><p>&nbsp;</p>
1 answer

Converting XML to JSON and JSON to XML by using Javascript or Java

 There are several tested solutions available on the Internet already: JAVASCRIPT Solution approach:  It provides 

  • XML to JSON

   Download Skript: http://www.thomasfrank.se/downloadableJS/xml2json.js  Usage as documented on this site: <head><script type="text/javascript" src="xml2json.js"></script>...</head>Then it can be called in these different variants: 

  1. V1: /*A simple call - myXML is a string containing your XML:*/ myJsonObject=xml2json.parser(myXML);
  2. V2: /*A 2:nd, optional, parameter is "tags not to convert" - for example <b> and <i>:*/ myJsonObject=xml2json.parser(myXML,'b,i')
  3. V3: /*A 3:rd, optional, parameter gives us a string showing us the JSON structure instead of the actual JSON object: */ myString=xml2json.parser(myXML,'','html');

 V1 ("compact") can be used for output without linebreaks or tabbing. V2 for normal output with linebreaks ant tabbing. V3 for HTML representation.JAVA solution approach  (which is solution I have already tested and which is provided by Json.org):    The Source files are available at http://json.org/java/ for many JSON issues in Java.    The relevant class is called XML and is viewable at:  

  It provides 

  • XML to JSON
  • JSON to XML

 

  1. Create your class
  2. Import org.json.XML;
  3. Prepare an xml input value: String xmlValue = "<..>";
  4. /*XML to JSON*/ Type: JSONObject jsonResult = XML.toJSONObject(xmlValue);
  5. /*JSON to XML*/ Type: String xmlResult = XML.toString(jsonResult);

  Naturally, the XML has to be well-formed and valid to produce a JSON object. The JSON objcect istself has to be valid again. It can be assumed that always correct outputs for valid inputs are retrieved according its high adoption in real world usage. 

Taggings: