staxon实现json和xml互转
2016-09-06 14:17
435 查看
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 { @Test public 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); } @Test public 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完美实现Xml与Json互转
- staxon 实现 json 与xml 之间的转换 支持数组,属性
- 使用 XMLHttpRequest 实现 Ajax 三:取回 Json 数据
- struts2之使用JSON插件实现Ajax(在struts.xml中处理要序列化的属性)
- Json-lib实现json,java对象,xml之间相互转换 .
- WebService中CXF实现响应数据在XML和JSON之间随意切换
- 用创建XMLHttpRequest对象与jQuery框架对文本为HTTP、JSON、XML的ajax实现比较
- jsonToxml 通过LL型方法实现(C#)[源]
- JAVA 实现XML与JSON 相互转换
- Json-lib 实现 json,java 对象,xml 之间相互转换
- XStream快速起步 简单方便实现序列化(XML和JSON)
- 利用XStream实现javabean到xml到json的互转
- 多叉树结合JavaScript树形控件实现无限级树形菜单(一种构建多级有序树形结构JSON(或XML)数据源的方法)
- 多叉树结合JavaScript树形控件实现无限级树形菜单(一种构建多级有序树形结构JSON(或XML)数据源的方法)
- xStream框架完美实现Java对象和xml文档JSON、XML相互转换
- Struts2实现异步调用机制详细剖析(XML和JSON)
- 使用xstream实现 xml json 对象互转
- xstream:实现JavaBean与XML/JSON的相互转换
- Android解析XML(PULL方式)和JSON 工作原理和实现过程(详细代码)
- iOS- 网络访问JSON数据类型与XML数据类型的实现思路及它们之间的区别