java將XML文檔轉(zhuǎn)換成json格式數(shù)據(jù)的示例
本文介紹了java將XML文檔轉(zhuǎn)換成json格式數(shù)據(jù)的示例,分享給大家,具體如下:
功能
將xml文檔轉(zhuǎn)換成json格式數(shù)據(jù)
說明
依賴包:
1. jdom-2.0.2.jar : xml解析工具包;
2. fastjson-1.1.36.jar : 阿里巴巴研發(fā)的高性能json工具包
程序源代碼
package com.xxx.open.pay.util; import com.alibaba.fastjson.JSONObject; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.LinkedList; import java.util.List; /** * Created by 1250052380@qq.com on 2015/5/19. */ public class XmlUtil { public static JSONObject xml2JSON(byte[] xml) throws JDOMException, IOException { JSONObject json = new JSONObject(); InputStream is = new ByteArrayInputStream(xml); SAXBuilder sb = new SAXBuilder(); org.jdom2.Document doc = sb.build(is); Element root = doc.getRootElement(); json.put(root.getName(), iterateElement(root)); return json; } private static JSONObject iterateElement(Element element) { List node = element.getChildren(); Element et = null; JSONObject obj = new JSONObject(); List list = null; for (int i = 0; i < node.size(); i++) { list = new LinkedList(); et = (Element) node.get(i); if (et.getTextTrim().equals("")) { if (et.getChildren().size() == 0) continue; if (obj.containsKey(et.getName())) { list = (List) obj.get(et.getName()); } list.add(iterateElement(et)); obj.put(et.getName(), list); } else { if (obj.containsKey(et.getName())) { list = (List) obj.get(et.getName()); } list.add(et.getTextTrim()); obj.put(et.getName(), list); } } return obj; } public static void main(String[] args) throws JDOMException, IOException { String xml="<?xml version=\"1.0\" encoding=\"utf-8\" ?><MoBaoAccount MessageType=\"UserMobilePay\" PlatformID=\"b2ctest\"><OrderNo>M20150521084825</OrderNo><TradeAmt>5000.00</TradeAmt><Commission>0.5</Commission><UserID>zhuxiaolong</UserID><MerchID>zhuxiaolong1</MerchID><tradeType>0</tradeType><CustParam>123</CustParam> <NotifyUrl>http://mobaopay.com/callback.do</NotifyUrl><TradeSummary>訂單</TradeSummary></MoBaoAccount>"; JSONObject json=xml2JSON(xml.getBytes()); System.out.println(json.toJSONString()); } }
執(zhí)行結(jié)果
XML原文:
<?xml version="1.0" encoding="utf-8" ?> <MoBaoAccount MessageType="UserMobilePay" PlatformID="b2ctest"> <OrderNo>M20150521084825</OrderNo> <TradeAmt>5000.00</TradeAmt> <Commission>0.5</Commission> <UserID>xiaolong</UserID> <MerchID>xiaolong1</MerchID> <tradeType>0</tradeType> <CustParam>123</CustParam> <NotifyUrl>http://mobaopay.com/callback.do</NotifyUrl> <TradeSummary>訂單</TradeSummary> </MoBaoAccount>
轉(zhuǎn)換后的json格式數(shù)據(jù)
{ "MoBaoAccount": { "Commission": [ "0.5" ], "CustParam": [ "123" ], "MerchID": [ "zhuxiaolong1" ], "NotifyUrl": [ "http://mobaopay.com/callback.do" ], "OrderNo": [ "M20150521084825" ], "TradeAmt": [ "5000.00" ], "TradeSummary": [ "訂單" ], "UserID": [ "zhuxiaolong" ], "tradeType": [ "0" ] } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Redis在微服務(wù)架構(gòu)中的幾種應(yīng)用場景
本文介紹在SpringCloud中使用Redis作為Pub/Sub異步通信、緩存或主數(shù)據(jù)庫和配置服務(wù)器的三種場景應(yīng)用。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05Java并發(fā)編程中的CyclicBarrier線程屏障詳解
這篇文章主要介紹了Java并發(fā)編程中的CyclicBarrier線程屏障詳解,2023-12-12解決使用redisTemplate高并發(fā)下連接池滿的問題
這篇文章主要介紹了解決使用redisTemplate高并發(fā)下連接池滿的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12使用Java程序模擬實(shí)現(xiàn)新冠病毒傳染效果
這篇文章主要介紹了用Java程序模擬實(shí)現(xiàn)新冠病毒傳染效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲的方法
這篇文章主要介紹了SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10