Skip to content

Instantly share code, notes, and snippets.

@wangshuai1992
Created March 14, 2019 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangshuai1992/21ebae9ece85d6e1c01c95cff60a7652 to your computer and use it in GitHub Desktop.
Save wangshuai1992/21ebae9ece85d6e1c01c95cff60a7652 to your computer and use it in GitHub Desktop.
XML fastjson互转
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
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;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* JSON、XML互转
*
* https://mvnrepository.com/artifact/de.odysseus.staxon/staxon
* <dependency>
* <groupId>de.odysseus.staxon</groupId>
* <artifactId>staxon</artifactId>
* <version>1.3</version>
* </dependency>
*
*
*/
public class JsonXmlUtils {
private static final Pattern BLANK = Pattern.compile("\\s*|\t|\r|\n");
/**
* convert json string to xml string
*
* @param json
* @return
*/
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) {
throw new RuntimeException(e);
} finally {
try {
output.close();
input.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return output.toString();
}
/**
* convert jsonObject to xml string
*
* @param jsonObject
* @return
*/
public static String jsonObj2xml(JSONObject jsonObject) {
return json2xml(jsonObject.toJSONString());
}
/**
* convert jsonObject to xml string / encoding="GBK"
*
* @param jsonObject
* @return
*/
public static String jsonObj2xmlGBK(JSONObject jsonObject) {
return json2xml(jsonObject.toJSONString()).replace("UTF-8", "GBK");
}
/**
* convert xml string to json string
*
* @param xml
* @return
*/
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) {
throw new RuntimeException(e);
} finally {
try {
output.close();
input.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return output.toString();
}
/**
* convert xml string to jsonObject
*
* @param xml
* @return
*/
public static JSONObject xml2jsonObj(String xml) {
return JSON.parseObject(xml2json(xml), Feature.OrderedField);
}
/**
* 去掉转换xml之后的换行和空格
*
* @param json
* @return
*/
public static String json2xmlReplaceBlank(String json) {
String str = JsonXmlUtils.json2xml(json);
String dest = "";
if (str != null) {
Matcher m = BLANK.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment