dacracot dacracot - 1 year ago 46
Java Question

Java implementation of JSON to XML conversion

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

Answer Source

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"
 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">

      "Mr Borat",

      "We're happy, you happy?"

        {"First Name":"Mary","sex":"Female"},
        {"First Name":"John","sex":"Male"},
        {"First Name":"Jackie","sex":"Female"}



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


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