staxon實現json和xml互轉

來源:互聯網
上載者:User

標籤:

pom.xml:

<dependency>    <groupId>de.odysseus.staxon</groupId>    <artifactId>staxon</artifactId>    <version>1.3</version></dependency>

轉換工具類:

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 */public 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() >= 38) {// 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();}}

測試代碼:

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\">鋼鐵是怎樣煉成的</name><name1 type=‘book‘ prices=‘100‘>鋼鐵是怎樣煉成的</name1><name type=‘book‘ prices=‘100‘>鋼鐵是怎樣煉成的</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\",\"$\":\"鋼鐵是怎樣煉成的\"},\"name1\":{\"@prices\":\"100\",\"@type\":\"book\",\"$\":\"鋼鐵是怎樣煉成的\"},\"name\":{\"@prices\":\"100\",\"@type\":\"book\",\"$\":\"鋼鐵是怎樣煉成的\"}}}";System.out.println(json);String xml = StaxonUtils.json2xml(json);System.out.println(xml);}}

  

 

staxon實現json和xml互轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.