java实现Xml与json之间的相互转换操作示例
作者:horizon~~~ 时间:2023-06-21 13:30:39
本文实例讲述了java实现Xml与json之间的相互转换操作。分享给大家供大家参考,具体如下:
旁白:
最近关于xml与json之间的转换都搞蒙了,这里写一个demo,以后备用。
正题:
project格式是:
jar包是一个一个检出来的,还算干净了。
代码:
工具类:
package exercise.xml;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import org.jdom.Document;
public class XmlExercise {
/**
* 将xml字符串<STRONG>转换</STRONG>为JSON字符串
*
* @param xmlString
* xml字符串
* @return JSON<STRONG>对象</STRONG>
*/
public static String xml2json(String xmlString) {
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xmlString);
return json.toString(1);
}
/**
* 将xmlDocument<STRONG>转换</STRONG>为JSON<STRONG>对象</STRONG>
*
* @param xmlDocument
* XML Document
* @return JSON<STRONG>对象</STRONG>
*/
public static String xml2json(Document xmlDocument) {
return xml2json(xmlDocument.toString());
}
/**
* JSON(数组)字符串<STRONG>转换</STRONG>成XML字符串
*
* @param jsonString
* @return
*/
public static String json2xml(String jsonString) {
XMLSerializer xmlSerializer = new XMLSerializer();
return xmlSerializer.write(JSONSerializer.toJSON(jsonString));
// return xmlSerializer.write(JSONArray.fromObject(jsonString));//这种方式只支持JSON数组
}
}
测试类:
package exercise.xml;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class XmlTest extends XmlExercise {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "horizon");
JSONArray jsonArray = new JSONArray();
JSONObject dataJson = new JSONObject();
jsonArray.add(jsonObject);
//jsonArray.add(jsonObject);
dataJson.put("data", jsonArray);
System.out.println(dataJson.toString());
String xml = json2xml(dataJson.toString());
System.out.println("xml:" + xml);
String str = xml2json(xml);
System.out.println("to_json" + str);
}
}
PS:这里再为大家提供几款相关在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformat
XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
希望本文所述对大家java程序设计有所帮助。
来源:https://www.cnblogs.com/tv151579/p/3516674.html
标签:java,Xml,json,相互转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# 动态加载dll文件,并实现调用其中的方法(推荐)
2021-11-10 04:37:34
浅析Java线程的中断机制
2023-11-19 20:11:50
java 中类似js encodeURIComponent 函数的实现案例
2023-03-20 13:13:50
unity使用射线实现贴花系统
2023-04-23 10:22:00
![](https://img.aspxhome.com/file/2023/4/108694_0s.jpg)
Java中的静态绑定和动态绑定详细介绍
2023-01-18 19:54:06
详解Java实现数据结构之并查集
2023-09-05 08:47:06
![](https://img.aspxhome.com/file/2023/6/67976_0s.jpg)
Netty分布式从recycler对象回收站获取对象过程剖析
2021-08-12 06:45:48
Spring Cloud Gateway 服务网关的部署与使用详细讲解
2022-04-09 01:20:00
![](https://img.aspxhome.com/file/2023/6/132196_0s.png)
Android之来电秀实战示例
2023-10-05 08:02:27
![](https://img.aspxhome.com/file/2023/5/137665_0s.png)
spring+html5实现安全传输随机数字密码键盘
2023-04-05 13:46:39
![](https://img.aspxhome.com/file/2023/7/125587_0s.jpg)
Java并发编程示例(一):线程的创建和执行
2022-01-24 16:43:49
java.exe和javaw.exe的区别及使用方法
2022-08-27 10:03:39
![](https://img.aspxhome.com/file/2023/0/113250_0s.png)
java实现简易外卖订餐系统
2021-11-04 16:01:25
![](https://img.aspxhome.com/file/2023/5/63415_0s.jpg)
Spring自动配置之condition条件判断下篇
2023-10-15 10:04:59
![](https://img.aspxhome.com/file/2023/0/62060_0s.png)
C#判断一个字符串是否是数字或者含有某个数字的方法
2022-05-07 14:38:17
MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)
2023-11-29 05:02:37
Spring Cloud Gateway集成Sentinel流控详情
2023-11-09 20:27:31
![](https://img.aspxhome.com/file/2023/2/86812_0s.png)
springboot集成es详解
2021-08-21 01:13:53
深入解读Android的内部进程通信接口AIDL
2022-09-09 04:02:02
详解java基于MyBatis使用示例
2023-11-25 09:01:45
![](https://img.aspxhome.com/file/2023/0/59060_0s.png)