java json xml app交互_JAVA-JSON、XML互转-【粗暴应用分享】

在Java开发中,JSON和XML互转常用。net.sf.json - lib.json - lib包有互转方法,FastJSON没有。本文分享一个用de.odysseus.staxon.staxon实现JSON与XML互转的工具类,给出代码并展示运行效果,帮助开发者解决JSON与XML互转难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实很多时候,我们只需要鱼,而不是渔,呐,给你鱼。

在平时的开发中,有时候会用到JSON和XML的互转

net.sf.json-lib.json-lib包提供一些互转的方法;

com.alibaba.fastjson并没有提供;

但是现在用FastJSON的人越来越多,好多人在面临到JSON到XML互转的时候还是有些束手无策,现在写一个特别好用的工具类,分享给大家,一如既往的粗暴,好用。

1、首先,推荐你用maven,然后不用多讲

de.odysseus.staxon

staxon

1.3

这个复制粘贴丢到pom.xml文件里面,然后开始直接丢代码:

/**

* @ClassName StaxonUtils

* @Description 实现JSON--XML互转

* @author watermelon_code

* @Date 2017年7月19日 上午10:49:48

* @version 1.0.0

*/

public class StaxonUtils {

/**

* @Description: json string convert to xml string

* @author watermelon_code

* @date 2017年7月19日 上午10:50:32

*/

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();

}

}

return output.toString();

}

/**

* @Description: json string convert to xml string ewidepay ues only

* @author watermelon_code

* @date 2017年7月19日 上午10:50:32

*/

public static String json2xmlPay(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();

}

/**

* @Description: xml string convert to json string

* @author watermelon_code

* @date 2017年7月19日 上午10:50:46

*/

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();

}

/**

* @Description: 去掉转换xml之后的换行和空格

* @author watermelon_code

* @date 2017年8月9日 下午4:05:44

*/

public static String json2xmlReplaceBlank(String json) {

String str = StaxonUtils.json2xml(json);

String dest = "";

if (str != null) {

Pattern p = Pattern.compile("\\s*|\t|\r|\n");

Matcher m = p.matcher(str);

dest = m.replaceAll("");

}

return dest;

}

}

看效果吧:

public static void main(String[] args) {

JSONObject json = new JSONObject();

json.put("name", "jack");

json.put("age", 25);

String xmlstr = "13488318601234567890123456";

System.out.println("JSON-->XML:");

System.out.println("JSON:" + json.toJSONString());

System.out.println("---------------------------------------------------------------");

System.out.println("普通转XML带格式:\n" + StaxonUtils.json2xml(json.toJSONString()));

System.out.println("---------------------------------------------------------------");

System.out.println("转XML去掉头部、前后补充:\n" + StaxonUtils.json2xmlPay(json.toJSONString()));

System.out.println("---------------------------------------------------------------");

System.out.println("普通转XML去掉空格换行:\n" + StaxonUtils.json2xmlReplaceBlank(json.toJSONString()));

System.out.println("---------------------------------------------------------------");

System.out.println("XML转JSON:\n" + StaxonUtils.xml2json(xmlstr));

}

运行结果:

JSON-->XML:

JSON:{"name":"jack","age":25}

---------------------------------------------------------------

普通转XML带格式:

jack

25

---------------------------------------------------------------

转XML去掉头部、前后补充:

jack

25

---------------------------------------------------------------

普通转XML去掉空格换行:

<?xmlversion ="1.0"encoding="UTF-8"?>jack25

---------------------------------------------------------------

XML转JSON:

{

"xml" : {

"ToUserName" : "toUser",

"FromUserName" : "fromUser",

"CreateTime" : 1348831860,

"MsgType" : "text",

"Content" : "thisisatest",

"MsgId" : 1234567890123456

}

}

那么这次的轮子到这里就结束了。

TO BE CONTINUE !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值