1. xml转json
1.1 代码目的
实现xml与json的互相转换,先实现xml -> json, 然后实现json -> xml字符串,最后xml字符串 -> 写入文件
xml结构时常会转换为json数据结构,感恩开源,有json的开源包帮忙解决问题。
1.2 代码实现
下面代码是xml转json的快速方法
public static string xmltojson() throws exception{
//使用dom4j
saxreader saxreader = new saxreader();
//读取文件
document read = saxreader.read("g:\\ideaprojects\\javastudy\\mooc\\src\\main\\resources\\score.xml");
//使用json的xml转json方法
jsonobject jsonobject = xml.tojsonobject(read.asxml());
//设置缩进转为字符串
system.out.println(jsonobject.tostring(3));
return jsonobject.tostring(3);
}
测试xml文件如下
<?xml version='1.0' encoding='utf-8'?>
<student>
<name>tom</name>
<subject>math</subject>
<score>80</score>
</student>
输出结果:
{"student":
{
"score": 80,
"subject": "math",
"name": "tom"
}
}
2. json转xml
//json转换成xml
public static string jsontoxml(string json){
//输入流
stringreader input = new stringreader(json);
//输出流
stringwriter output = new stringwriter();
//构建配置文件
jsonxmlconfig config = new jsonxmlconfigbuilder().multiplepi(false).repairingnamespaces(false).build();
try {
//xml事件读
// this is the top level interface for parsing xml events. it provides
// the ability to peek at the next event and returns configuration
// information through the property interface.
// 这是最解析xml事件最顶层的接口,它提供了查看下一个事件并通过属性界面返回配置信息的功能。
xmleventreader reader = new jsonxmlinputfactory(config).createxmleventreader(input);
//这是编写xml文档的顶级界面。
//验证xml的形式不需要此接口的实例。
xmleventwriter writer = xmloutputfactory.newinstance().createxmleventwriter(output);
//创建一个实例使用默认的缩进和换行
writer = new prettyxmleventwriter(writer);
//添加整个流到输出流,调用next方法,知道hasnext返回false
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) {
system.out.println(output.tostring().substring(39));
return output.tostring().substring(39);
}
system.out.println(output);
return output.tostring();
}
传入的json字符串即上xml转json中输出的,json转xml的结果如下:
此时是没有头部,在文中被移除!
<student>
<score>80</score>
<subject>math</subject>
<name>tom</name>
</student>
3. json转xml并输出成指定的文件
传入xml字符串,利用dom4j工具即可输出到指定的文件
public static void writexmltofile(string xmlstr) throws exception{
//将xmlstr转为文件形式
document document = documenthelper.parsetext(xmlstr);
//设置输出的格式
outputformat format = outputformat.createprettyprint();
//构建输出流
xmlwriter xmlwriter = new xmlwriter(new fileoutputstream("newxml.xml"), format);
//不要转义字符
xmlwriter.setescapetext(false);
//写入
xmlwriter.write(document);
//关闭流
xmlwriter.close();
}
4. 主要的pom.xml配置如下
如下的依赖就足够了
<dependency>
<groupid>org.json</groupid>
<artifactid>json</artifactid>
<version>20171018</version>
</dependency>
<dependency>
<groupid>dom4j</groupid>
<artifactid>dom4j</artifactid>
<version>1.6.1</version>
</dependency>
<dependency>
<groupid>de.odysseus.staxon</groupid>
<artifactid>staxon</artifactid>
<version>1.3</version>
</dependency>
5. 整体代码
public class main {
public static void main(string[] args)throws exception {
//将xml转化成json
string jsonstr = xmltojson();
//将json转换成xml
string xmlstr = jsontoxml(jsonstr);
//将json按照响应格式写入score2.xml
writexmltofile(xmlstr);
}
public static string xmltojson() throws exception{
//使用dom4j
saxreader saxreader = new saxreader();
//读取文件
document read = saxreader.read("g:\\ideaprojects\\javastudy\\mooc\\src\\main\\resources\\score.xml");
//使用json的xml转json方法
jsonobject jsonobject = xml.tojsonobject(read.asxml());
//设置缩进转为字符串
system.out.println(jsonobject.tostring(3));
return jsonobject.tostring(3);
}
public static void writexmltofile(string xmlstr) throws exception{
//将xmlstr转为文件形式
document document = documenthelper.parsetext(xmlstr);
//设置输出的格式
outputformat format = outputformat.createprettyprint();
//构建输出流
xmlwriter xmlwriter = new xmlwriter(new fileoutputstream("g:\\ideaprojects\\javastudy\\mooc\\src\\main\\resources\\score2.xml"), format);
//不要转义字符
xmlwriter.setescapetext(false);
//写入
xmlwriter.write(document);
//关闭流
xmlwriter.close();
}
//json转换成xml
public static string jsontoxml(string json){
//输入流
stringreader input = new stringreader(json);
//输出流
stringwriter output = new stringwriter();
//构建配置文件
jsonxmlconfig config = new jsonxmlconfigbuilder().multiplepi(false).repairingnamespaces(false).build();
try {
//xml事件读
// this is the top level interface for parsing xml events. it provides
// the ability to peek at the next event and returns configuration
// information through the property interface.
// 这是最解析xml事件最顶层的接口,它提供了查看下一个事件并通过属性界面返回配置信息的功能。
xmleventreader reader = new jsonxmlinputfactory(config).createxmleventreader(input);
//这是编写xml文档的顶级界面。
//验证xml的形式不需要此接口的实例。
xmleventwriter writer = xmloutputfactory.newinstance().createxmleventwriter(output);
//创建一个实例使用默认的缩进和换行
writer = new prettyxmleventwriter(writer);
//添加整个流到输出流,调用next方法,知道hasnext返回false
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) {
system.out.println(output.tostring().substring(39));
return output.tostring().substring(39);
}
system.out.println(output);
return output.tostring();
}
}
到此这篇关于java实现xml与json的互相转换详解的文章就介绍到这了,更多相关java xml与json互转内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论