首先
<!-- https://mvnrepository.com/artifact/de.odysseus.staxon/staxon -->
<dependency>
<groupId>de.odysseus.staxon</groupId>
<artifactId>staxon</artifactId>
<version>1.3</version>
</dependency>
上代码
package Test.test;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
/**
* @ClassName JsonUtils
* @Description 实现JSON--XML互转
* @Date 2019.8.27
*/
public class JsonUtils {
/**
* @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 "<xml>" + output.toString().substring(39) + "</xml>";
}
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 = JsonUtils.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) {
String json = "{\"business\":{\"body\":{\"fpdm\":\"发票代码\",\"fplxdm\":\"发票类型代码\",\"kpzdbs\":\"开票终端标识\",\"dyfs\":\"打印方式\",\"dylx\":\"打印类型\",\"printername\":\"打印机名称\",\"fphm\":\"发票号码\"}}}";
String xml = "<?xml version=\"1.0\" encoding=\"gbk\"?>" +
"<business>" +
"<body >" +
"<kpzdbs>开票终端标识</kpzdbs>" +
"<fplxdm>发票类型代码</fplxdm>" +
"<fpdm>发票代码</fpdm>" +
"<fphm>发票号码</fphm>" +
"<dylx>打印类型</dylx>" +
"<dyfs>打印方式</dyfs>" +
"<printername>打印机名称</printername>" +
"</body>" +
"</business>";
System.out.println(json2xml(json));
System.out.println(xml2json(xml));
}
}