staxon 实现 json 与xml 之间的转换 支持数组,属性
package org;
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;
/**
* json and xml converter
* @author magic_yy
* @see https://github.com/beckchr/staxon
* @see https://github.com/beckchr/staxon/wiki
*
*/
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();
}
}
测试:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import com.alibaba.fastjson.JSONObject;
public class TestMainNew {
public static void main(String[] args) {
// TODO Auto-generated method stub
String xml = StaxonUtils.json2xml(getContent("resource/json2xml.txt", "utf-8"));
System.out.println(xml);
String json = StaxonUtils.xml2json(getContent("resource/BB.xml", "utf-8"));
System.out.println(json);
JSONObject jsonObject =JSONObject.parseObject(json);
System.out.println(jsonObject.get("ClinicalDocument"));
// writeFile("d:\\json2xml.txt", json, "UTF-8");//保存文件
}
public static String getContent(String pathName,String charsetName){
StringBuffer jsonBuffer = new StringBuffer();
try {
InputStream is = new FileInputStream(pathName);//文件是DOS 格式
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is,charsetName));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
jsonBuffer.append(line); // 将读到的内容添加到 buffer 中
jsonBuffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonBuffer.toString();
}
/**
* 写入文件
* @param filePathAndName String 如 c:\\1.txt 绝对路径
* @param fileContent 文件内容
* @param charsetName 编码格式
*/
public static void writeFile(String filePathAndName, String fileContent,String charsetName) {
try {
File f = new File(filePathAndName);
if (!f.exists()) {
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),charsetName);
BufferedWriter writer=new BufferedWriter(write);
writer.write(fileContent);
writer.close();
} catch (Exception e) {
System.out.println("写文件内容操作出错");
e.printStackTrace();
}
}
}
用到 了包staxon-1.3.jar
svn:http://maven-eu.nuxeo.org/nexus/content/repositories/thirdparty-releases/de/odysseus/staxon/staxon/1.3/ 可下载源码,jar 和说明文档
完整工程下载地址:
http://download.csdn.net/detail/luoww1/9319129