生活随笔
收集整理的這篇文章主要介紹了
java json与xml互转工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
部分代碼參考:https://blog.csdn.net/CDWLX/article/details/119038509
工具類
public class XmlMutualConversionJsonUtil {public static void main(String[] args
) throws Exception {String xml
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ "<root>"+ " <mdcardno>查詢卡號</mdcardno>"+ " <count>返回明細條數</count>"+ " <rd1>"+ " <test>"+ " <trxzone>交易地區號1</trxzone>"+ " <trxcurr>交易幣種1</trxcurr>"+ " </test>"+ " </rd1>"+ " <rd>"+ " <trxzone>交易地區號2</trxzone>"+ " <trxcurr>交易幣種2</trxcurr>"+ " </rd>"+ " <rd>"+ " <trxzone>交易地區號3</trxzone>"+ " <trxcurr>交易幣種3</trxcurr>"+ "</rd>"+ "</root>";JSONObject jsonObject
=toJson(xml
);System.out
.println(jsonObject
);System.out
.println("----------------");Element root
=new BaseElement("root");Element element
= toXml(jsonObject
.toJSONString(), root
);System.out
.println(element
.asXML());}public static JSONObject toJson(String xml
){JSONObject jsonObject
= new JSONObject();Document document
= null;try {document
= DocumentHelper.parseText(xml
);} catch (DocumentException e
) {e
.printStackTrace();}Element root
= document
.getRootElement();return xmlToJson(root
,jsonObject
);}public static JSONObject xmlToJson(Element node
,JSONObject json
){List<Element> list
= node
.elements();String name
= node
.getName();if(list
.isEmpty()){String nodeValue
= node
.getTextTrim();json
.put(name
, nodeValue
);}else{JSONObject js
= new JSONObject();if(json
.containsKey(name
)){JSONArray jsonArray
= null;Object o
= json
.get(name
);if(o
instanceof JSONArray){jsonArray
=(JSONArray) o
;}else{jsonArray
= new JSONArray();jsonArray
.add(o
);}json
.put(name
,jsonArray
);jsonArray
.add(js
);}else {json
.put(name
,js
);}for (Element element
: list
) {xmlToJson(element
,js
);}}return json
;}public static Element toXml(String json
,Element root
) {JsonObject jsonObject
= new JsonParser().parse(json
).getAsJsonObject();Element ee
= jsonToXml(jsonObject
, root
, null);return ee
.elements().get(0);}public static Element jsonToXml(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
);jsonToXml(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
) {jsonToXml(s
.getValue(), currentElement
!= null ? currentElement
: parentElement
, s
.getKey());}} else {Element el
= parentElement
.addElement(name
);el
.addText(jsonElement
.getAsString());}return parentElement
;}}
依賴 jar包
<dependency><groupId>org
.dom4j
</groupId
><artifactId>dom4j
</artifactId
><version>2.1.3</version
></dependency
><!-- https
://mvnrepository
.com
/artifact
/com
.alibaba
/fastjson
--><dependency><groupId>com
.alibaba
</groupId
><artifactId>fastjson
</artifactId
><version>1.2.83</version
></dependency
><dependency><groupId>com
.google
.code
.gson
</groupId
><artifactId>gson
</artifactId
></dependency
>
總結
以上是生活随笔為你收集整理的java json与xml互转工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。