Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java implementation of JSON to XML conversion [closed]

Are there existing JARs available to convert from JSON to XML?

like image 253
dacracot Avatar asked Feb 18 '09 00:02

dacracot


People also ask

Can we convert JSON to XML in Java?

We can convert a JSONObject into an XML format using org. json. XML class, this provides static methods to convert an XML text into a JSONObject and to convert a JSONObject into an XML text.

Can you convert JSON to XML?

To convert a JSON document to XML, follow these steps: Select the JSON to XML action from the Tools > JSON Tools menu. Choose or enter the Input URL of the JSON document. Choose the path of the Output file that will contain the resulting XML document.

How do you keep spaces in the tags when converting XML to JSON in Java?

How can I keep spaces in strings when converting? Those spaces are unsignificant and would be removed by many tools. You can try adding the xml:space="preserve" attribute to the element, or enclosing the text inside a <!

Which utility used by REST services convert JSON format to XML?

The serializeMapToXML utility. REST services convert JSON format to XML format using the com.


2 Answers

You can create a JSONObject, and then convert it to XML using the XML class in the org.json namespace

Wrapping the json string in the object is as easy as passing it in its constructor

JSONObject o = new JSONObject(jsonString); 

Then you can get it in XML format using the XML class, like so:

String xml = org.json.XML.toString(o); 
like image 171
juan Avatar answered Sep 23 '22 21:09

juan


Not a Java, but a pure XSLT 2.0 implementation:

Have a look at the f:json-document() from the FXSL 2.x library.

Using this function it is extremely easy to incorporate JSon and use it just as... XML.

For example, one can just write the following XPath expression:

f:json-document($vstrParam)/Students/*[sex = 'Female']

and get all children of Students with sex = 'Female'

Here is the complete example:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="f xs"
 >
 <xsl:import href="../f/func-json-document.xsl"/>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vstrParam" as="xs:string">
{

  "teacher":{
    "name":
      "Mr Borat",
    "age":
      "35",
    "Nationality":
      "Kazakhstan"
             },


  "Class":{
    "Semester":
      "Summer",
    "Room":
      null,
    "Subject":
      "Politics",
    "Notes":
      "We're happy, you happy?"
           },

  "Students":
    {
      "Smith":
        {"First Name":"Mary","sex":"Female"},
      "Brown":
        {"First Name":"John","sex":"Male"},
      "Jackson":
        {"First Name":"Jackie","sex":"Female"}
    }
    ,


  "Grades":

    {
      "Test":
      [
        {"grade":"A","points":68,"grade":"B","points":25,"grade":"C","points":15},

        {"grade":"C","points":2, "grade":"B","points":29, "grade":"A","points":55},

        {"grade":"C","points":2, "grade":"A","points":72, "grade":"A","points":65}
       ]
    }


}
 </xsl:variable>

 <xsl:template match="/">
    <xsl:sequence select=
     "f:json-document($vstrParam)/Students/*[sex = 'Female']"/>

 </xsl:template>
</xsl:stylesheet>

When the above transformation is applied on any XML document (ignored), the correct result is produced:

<Smith>
   <First_Name>Mary</First_Name>
   <sex>Female</sex>
</Smith>
<Jackson>
   <First_Name>Jackie</First_Name>
   <sex>Female</sex>
</Jackson>
like image 25
Dimitre Novatchev Avatar answered Sep 24 '22 21:09

Dimitre Novatchev