日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java json与xml互转工具类

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java json与xml互转工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

部分代碼參考:https://blog.csdn.net/CDWLX/article/details/119038509
工具類

public class XmlMutualConversionJsonUtil {/*** 測試的main方法*/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){//獲取子節點listList<Element> list = node.elements();//獲取節點名字String name = node.getName();//最下面的一層if(list.isEmpty()){String nodeValue = node.getTextTrim();json.put(name, nodeValue);}else{//下級節點進行嵌套JSONObject js = new JSONObject();//判斷json數據中是否存在相同的 key//存在相同的key需要使用數組存儲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;}/*** 將json字符串轉換成xml** @param json* json字符串* @throws Exception*/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);}/*** 將json字符串轉換成xml** @param jsonElement* 待解析json對象元素* @param parentElement* 上一層xml的dom對象* @param name* 父節點*/public static Element jsonToXml(JsonElement jsonElement, Element parentElement, String name) {if (jsonElement instanceof JsonArray) {//是json數據,需繼續解析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) {//說明是一個json對象字符串,需要繼續解析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 {//說明是一個鍵值對的key,可以作為節點插入了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互转工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。