Java -> Json -> xml 格式之间转换
需求
获取xml报文 读取xml数据转换json 推送json数据给第三方
生成对应xml报文 ftp提交服务器
json-lib和fastjson在实现时都出现了意外情况后放弃
json转xml时不识别对应标签 单个数组标签会丢失数组变成对象
对接海关xml报文需求时 标签顺序乱了
转换不了命名空间属性和普通标签属性
记录dom4j gson实现记录
记录参数配置
普通参数 例: head 、body
属性参数和命令空间参数: -xmlns
根元素: sourceData
实现 xml中标签属性的插入 和命令空间属性的插入 webservice例子如下
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
<soapenv:Header/>
<soapenv:Body>
<web:getSupportCity xmlns:web="http://WebXml.com.cn/">
<web:byProvinceName xmlns:web="http://WebXml.com.cn/">广东</web:byProvinceName>
</web:getSupportCity>
</soapenv:Body>
</soapenv:Envelope>
普通xml报文如下
<Data xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
<Header/>
<Body>
<Student type="get">
<name>mw</name>
</Student>
</Body>
</Data>
public static String jsonToXml(Object json, String rootKey) {
Document document = DocumentHelper.createDocument();
QName qName = new QName(rootKey);
Element element = document.addElement(qName);
document.setRootElement(element);
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonObject jsonObject = new JsonParser().parse(objectMapper.writeValueAsString(json)).getAsJsonObject();
toXml(jsonObject, document.getRootElement(), null);
return document.asXML();
} catch (Exception e) {
log.error(e.getMessage());
throw new AppException(e.getMessage());
}
}
private static Element toXml(JsonElement jsonElement, Element parentElement, String name) {
if (jsonElement instanceof JsonArray) {
JsonArray sonJsonArray = (JsonArray)jsonElement;
for (int i = 0; i < sonJsonArray.size(); i++) {
JsonElement arrayElement = sonJsonArray.get(i);
toXml(arrayElement, parentElement, name);
}
}else if (jsonElement instanceof JsonObject) {
JsonObject sonJsonObject = (JsonObject) jsonElement;
Element currentElement = null;
if (name != null) {
currentElement = parentElement.addElement(name);
}
Set<Map.Entry<String, JsonElement>> set = sonJsonObject.entrySet();
for (Map.Entry<String, JsonElement> s : set) {
toXml(s.getValue(), currentElement != null ? currentElement : parentElement, s.getKey());
}
}else if (jsonElement.isJsonNull()){
parentElement.addElement(name);
}else {
addAttribute(parentElement, name, jsonElement.getAsString());
}
return parentElement;
}
private static void addAttribute(Element element, String name, String value) {
if (String.valueOf(name.charAt(0)).equals(StringPool.DASH)){
String key=name.substring(1);
if (key.equals("xmlns")){
Namespace namespace=new Namespace("",value);
QName qName=new QName(element.getName(),namespace);
element.setQName(qName);
}else {
element.addAttribute(key,value);
}
}else {
Element el = element.addElement(name);
el.addText(value);
}
}
额外记录 mybatisplus StringPool常用符号常量
String AMPERSAND = "&";
String AND = "and";
String AT = "@";
String ASTERISK = "*";
String STAR = "*";
String BACK_SLASH = "\\";
String COLON = ":";
String COMMA = ",";
String DASH = "-";
String DOLLAR = "$";
String DOT = ".";
String DOTDOT = "..";
String DOT_CLASS = ".class";
String DOT_JAVA = ".java";
String DOT_XML = ".xml";
String EMPTY = "";
String EQUALS = "=";
String FALSE = "false";
String SLASH = "/";
String HASH = "#";
String HAT = "^";
String LEFT_BRACE = "{";
String LEFT_BRACKET = "(";
String LEFT_CHEV = "<";
String DOT_NEWLINE = ",\n";
String NEWLINE = "\n";
String N = "n";
String NO = "no";
String NULL = "null";
String OFF = "off";
String ON = "on";
String PERCENT = "%";
String PIPE = "|";
String PLUS = "+";
String QUESTION_MARK = "?";
String EXCLAMATION_MARK = "!";
String QUOTE = "\"";
String RETURN = "\r";
String TAB = "\t";
String RIGHT_BRACE = "}";
String RIGHT_BRACKET = ")";
String RIGHT_CHEV = ">";
String SEMICOLON = ";";
String SINGLE_QUOTE = "'";
String BACKTICK = "`";
String SPACE = " ";
String TILDA = "~";
String LEFT_SQ_BRACKET = "[";
String RIGHT_SQ_BRACKET = "]";
String TRUE = "true";
String UNDERSCORE = "_";
String UTF_8 = "UTF-8";
String US_ASCII = "US-ASCII";
String ISO_8859_1 = "ISO-8859-1";
String Y = "y";
String YES = "yes";
String ONE = "1";
String ZERO = "0";
String DOLLAR_LEFT_BRACE = "${";
String HASH_LEFT_BRACE = "#{";
String CRLF = "\r\n";
String HTML_NBSP = " ";
String HTML_AMP = "&";
String HTML_QUOTE = """;
String HTML_LT = "<";
String HTML_GT = ">";
String[] EMPTY_ARRAY = new String[0];
byte[] BYTES_NEW_LINE = "\n".getBytes();
|