一、xml格式转换成json格式
package com.herocheer.bms.egjjfw.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import java.util.List;
public class xmlAndJson {
public static void main(String[] args) {
try {
String rest = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
"\n" +
"<ROOT>\n" +
" <HEAD>\n" +
" <DemoCode></DemoCode>\n" +
" <ChanCode>11</ChanCode>\n" +
" <DemoCent></DemoCent>\n" +
" <DemoGlb></DemoGlb>\n" +
" <DemoBank></DemoBank>\n" +
" <DemoTell></DemoTell>\n" +
" <Code>000</Code>\n" +
" <Msg>交易成功</Msg>\n" +
" <Date>20200909</Date>\n" +
" <Time>093035</Time>\n" +
" <TrsCode>6029</TrsCode>\n" +
" <TrsChild></TrsChild>\n" +
" <DemoSerial>1599615034960</DemoSerial>\n" +
" </HEAD>\n" +
" <BODY>\n" +
" <total>500</total>\n" +
" <pagesize>19</pagesize>\n" +
" <rows>\n" +
" <xh>70</xh>\n" +
" <demo>1310000000</demo>\n" +
" <company>阿里巴巴</company>\n" +
" <zxjedhwe>C11112</zxjedhwe>\n" +
" </rows>\n" +
" <rows>\n" +
" <xh>71</xh>\n" +
" <demo>13110000000</demo>\n" +
" <company>宇宙无敌天下商会</company>\n" +
" <zxjedhwe>C11114</zxjedhwe>\n" +
" </rows>\n" +
" <rows>\n" +
" <xh>72</xh>\n" +
" <demo>140900000</demo>\n" +
" <company>娃哈哈有限公司</company>\n" +
" <zxjedhwe>C11111</zxjedhwe>\n" +
" </rows>\n" +
" </BODY>\n" +
"</ROOT>";
JSONObject jsonObject = xml2Json(rest);
System.out.println(jsonObject);
} catch (DocumentException e) {
e.printStackTrace();
}
}
/** * xml转json * @param xmlStr * @return * @throws DocumentException */
public static JSONObject xml2Json(String xmlStr) throws DocumentException {
Document doc = DocumentHelper.parseText(xmlStr);
JSONObject json = new JSONObject();
dom4j2Json(doc.getRootElement(), json);
return json;
}
public static void dom4j2Json(Element element, JSONObject json) {
// 如果是属性
for (Object o : element.attributes()) {
Attribute attr = (Attribute) o;
if (StringUtils.isNotEmpty(attr.getValue())) {
json.put("@" + attr.getName(), attr.getValue());
}
}
List<Element> chdEl = element.elements();
if (chdEl.isEmpty() && StringUtils.isNotEmpty(element.getText())) { // 如果没有子元素,只有一个值
json.put(element.getName(), element.getText());
}
for (Element e : chdEl) { // 有子元素
if (!e.elements().isEmpty()) { // 子元素也有子元素
JSONObject chdjson = new JSONObject();
dom4j2Json(e, chdjson);
Object o = json.get(e.getName());
if (o != null) {
JSONArray jsona = null;
if (o instanceof JSONObject) { // 如果此元素已存在,则转为jsonArray
JSONObject jsono = (JSONObject) o;
json.remove(e.getName());
jsona = new JSONArray();
jsona.add(jsono);
jsona.add(chdjson);
}
if (o instanceof JSONArray) {
jsona = (JSONArray) o;
jsona.add(chdjson);
}
json.put(e.getName(), jsona);
} else {
if (!chdjson.isEmpty()) {
json.put(e.getName(), chdjson);
}
}
} else { // 子元素没有子元素
for (Object o : element.attributes()) {
Attribute attr = (Attribute) o;
if (StringUtils.isNotEmpty(attr.getValue())) {
json.put("@" + attr.getName(), attr.getValue());
}
}
if (!e.getText().isEmpty()) {
json.put(e.getName(), e.getText());
}
}
}
}
}
控制台输出:
{ "HEAD":{ "Msg":"交易成功","ChanCode":"11","Time":"093035","TrsCode":"6029","DemoSerial":"1599615034960","Code":"000","Date":"20200909"},
"BODY":{ "total":"500","pagesize":"19","rows":[{ "xh":"70","zxjedhwe":"C11112","company":"阿里巴巴","demo":"1310000000"},
{ "xh":"71","zxjedhwe":"C11114","company":"宇宙无敌天下商会","demo":"13110000000"},
{ "xh":"72","zxjedhwe":"C11111","company":"娃哈哈有限公司","demo":"140900000"}]}}
还没有评论,来说两句吧...