Pom.xml:
<dependency> <groupId>de.odysseus.staxon</groupId> <artifactid>staxon</ Artifactid> <version>1.3</version></dependency>
Conversion Tool Classes:
Package Com.nihaorz.utils;import Java.io.ioexception;import Java.io.stringreader;import java.io.StringWriter; Import Javax.xml.stream.xmleventreader;import Javax.xml.stream.xmleventwriter;import Javax.xml.stream.xmlinputfactory;import Javax.xml.stream.xmloutputfactory;import De.odysseus.staxon.json.jsonxmlconfig;import De.odysseus.staxon.json.jsonxmlconfigbuilder;import De.odysseus.staxon.json.jsonxmlinputfactory;import De.odysseus.staxon.json.jsonxmloutputfactory;import De.odysseus.staxon.xml.util.prettyxmleventwriter;public class Staxonutils {/** * JSON string Convert to XML string */PUBL IC static string Json2xml (String json) {StringReader input = new StringReader (JSON); StringWriter output = new StringWriter (); Jsonxmlconfig config = new Jsonxmlconfigbuilder (). Multiplepi (False). Repairingnamespaces (False). build (); try { Xmleventreader reader = new jsonxmlinputfactory (config). Createxmleventreader (input); Xmleventwriter writer = Xmloutputfactory.newinstance (). Createxmleventwriter (output); WRIter = new Prettyxmleventwriter (writer); Writer.add (reader); Reader.close (); Writer.close ();} catch (Exception e) {e.printstacktrace ();} finally {try {output.close (); Input.close ();} catch (IOException e) { E.printstacktrace ();}} if (output.tostring (). Length () >=) {//Remove <?xml version= "1.0" encoding= "UTF-8"? >return output.tostring ( ). substring (39);} return output.tostring ();} /** * XML string convert to JSON string */public static string Xml2json (String xml) {StringReader input = new StringReader (XML); StringWriter output = new StringWriter (); Jsonxmlconfig config = new Jsonxmlconfigbuilder (). Autoarray (True). Autoprimitive (True). Prettyprint (True). Build (); try {xmleventreader reader = Xmlinputfactory.newinstance (). Createxmleventreader (input); Xmleventwriter writer = new jsonxmloutputfactory (config). Createxmleventwriter (output); Writer.add (reader); Reader.close (); Writer.close ();} catch (Exception e) {e.printstacktrace ();} finally {try {output.close (); Input.close ();} catch (IOException e){E.printstacktrace ();}} return output.tostring ();}}
Test code:
Package Com.nihaorz.xmltojson;import Org.junit.test;import Com.nihaorz.utils.staxonutils;public class UtilsTest {@ testpublic void Test_xmltojson () {String xml = "<goods><name type=\" Book\ "prices=\" 100\ "> How steel is tempered </ Name><name1 type= ' book ' prices= ' > How the Steel is tempered </name1><name type= ' book ' prices= ' ['] > How the Steel is tempered </name></goods> "; SYSTEM.OUT.PRINTLN (XML); String JSON = Staxonutils.xml2json (XML); SYSTEM.OUT.PRINTLN (JSON);} @Testpublic void Test_jsontoxml () {String json = "{\" goods\: {\ "name\": {\ "@prices \": 100,\ "@type \": \ "Book\", \ "$\": \ " How the steel is refined into the "},\" name1\ ": {\" @prices \ ": \" 100\ ", \" @type \ ": \" Book\ ", \" $\ ": \" How the steel is refined "},\" name\ ": {\" @prices \ ": \" 100\ ", \" @type \ ": \" Book\ ", \" $\ ": \" How the Steel is refined "}}"; SYSTEM.OUT.PRINTLN (JSON); String XML = Staxonutils.json2xml (JSON); SYSTEM.OUT.PRINTLN (XML);}}
Staxon implementing JSON and XML interoperability