json报文转化为xml报文_JSON与XML互相转化(Jackson)
選型: json-lib.jar可以做,但是太老了,拋棄; fastjson完全沒有xml相關(guān)功能; staxon經(jīng)測試不能用。 最終選用jackson來主要實(shí)現(xiàn)。
JDK為1.8,如果低于1.8,則只需要將try-catch的語法改回傳統(tǒng)書寫方式。
目前的實(shí)現(xiàn),傳參和回參都是String,如果想JSONObject對象和XML對象互轉(zhuǎn),只需要再包一層就可以多2個或4個方法。
一、依賴
pom.xml
com.fasterxml.jackson.core
jackson-core
2.8.8
com.fasterxml.jackson.core
jackson-databind
2.8.8
com.fasterxml.jackson.dataformat
jackson-dataformat-xml
2.8.8
com.alibaba
fastjson
1.2.33
二、實(shí)現(xiàn)
package com.kapu.common.utility;
import java.io.IOException;
import java.io.StringWriter;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class JsonXmlUtil {
private static final String XML_PREFIX = "<?xml version="1.0" encoding="UTF-8" ?>";
private static final String XML_PREFIX_HEAD = "";
private static final String XML_PREFIX_TAIL = "?>";
private static final String XML_ROOT_START = "";
private static final String XML_ROOT_END = "";
/**
* json string convert to xml string
* @throws IOException
* @throws JsonParseException
*/
public static String json2xml(String json) throws IOException {
if (StringUtils.isEmpty(json)) {
return null;
}
JSONObject jsonObject = JSON.parseObject(json);
StringWriter w = new StringWriter();
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(w, jsonObject);
String xml = w.toString();
xml = xml.replaceAll("", "").replaceAll("", "");
xml = XML_PREFIX + xml;
return xml;
}
/**
* xml string convert to json string
* @throws IOException
*/
public static String xml2json(String xml) throws IOException {
if(StringUtils.isEmpty(xml)) {
return null;
}
if(xml.indexOf(XML_PREFIX_HEAD) > -1 && xml.indexOf(XML_PREFIX_TAIL) > -1){
xml = xml.substring(xml.indexOf(XML_PREFIX_TAIL) + 2);
}
xml = XML_ROOT_START + xml + XML_ROOT_END;
StringWriter w = new StringWriter();
try(JsonParser jp = new XmlMapper().getFactory().createParser(xml);JsonGenerator jg = new ObjectMapper().getFactory().createGenerator(w);){
while (jp.nextToken() != null) {
jg.copyCurrentEvent(jp);
}
}
return w.toString();
}
}
三、測試
package com.kapu.common.base;
import java.io.IOException;
import org.junit.Test;
import com.kapu.common.utility.JsonXmlUtil;
import com.alibaba.fastjson.JSONObject;
public class JsonXml {
@Test
public void testJson2Xml() {
JSONObject meta = new JSONObject();
meta.put("from", "WEB");
meta.put("token", "sdfa4sdf4as5d4f8a4sd8f4asdf");
meta.put("timestamp", System.currentTimeMillis());
JSONObject data = new JSONObject();
data.put("id", 123);
data.put("name", "one-two-three");
data.put("desc", "this is a number list");
JSONObject json = new JSONObject();
json.put("meta", meta);
json.put("data", data);
json.put("aaa", "aaaaaa");
System.out.println(json);
String xml = null;
try {
xml = JsonXmlUtil.json2xml(json.toJSONString());
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println(xml);
}
@Test
public void testXml2Json() {
String xml = "<?xml version="1.0" encoding="UTF-8" ?>aaaaaaone-two-three123thisisanumberlistWEBsdfa4sdf4as5d4f8a4sd8f4asdf1536049559684";
try {
String json = JsonXmlUtil.xml2json(xml);
System.out.println(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testJson2Xml2Json() throws IOException {
JSONObject meta = new JSONObject();
meta.put("from", "WEB");
meta.put("token", "sdfa4sdf4as5d4f8a4sd8f4asdf");
meta.put("timestamp", System.currentTimeMillis());
JSONObject data = new JSONObject();
data.put("id", 123);
data.put("name", "one-two-three");
data.put("desc", "this is a number list");
JSONObject json = new JSONObject();
json.put("meta", meta);
json.put("data", data);
json.put("aaa", "aaaaaa");
System.out.println(json.toJSONString());
String xml = JsonXmlUtil.json2xml(json.toJSONString());
System.out.println(xml);
String jsonStr = JsonXmlUtil.xml2json(xml);
System.out.println(jsonStr);
}
}
總結(jié)
以上是生活随笔為你收集整理的json报文转化为xml报文_JSON与XML互相转化(Jackson)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php分布式微服务开发_分布式微服务架构
- 下一篇: 逾期怎么处理_招商信用卡逾期三个月银行起