Staxon implementing JSON and XML interoperability

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.