In diesem Artikel wird ein Beispiel dafür vorgestellt, dass Java XML -Dokumente in JSON -Formatdaten umwandelt. Es wird mit Ihnen geteilt. Die Details sind wie folgt:
Funktion
Konvertieren Sie XML -Dokumente in JSON -Formatdaten
veranschaulichen
Abhängigkeitspaket:
1. Jdom-2.0.2.jar: XML Parsing Toolkit;
2. Fastjson-1.1.36.Jar: Ein von Alibaba entwickelter Hochleistungs-JSON-Toolkit, der entwickelt wurde
Programmquellcode
Paket 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.ByTexputStream; java.util.linkedList; import java.util.list;/*** Erstellt von [email protected] unter 2015/5/19. */public class xmlutil {public static jsonObject xml2json (byte [] xml) löst Jdomexception aus, ioException {jsonObject json = new JsonObject (); InputStream ist = neuer BytearrayInputStream (XML); Saxbuilder SB = neuer Saxbuilder (); org.jdom2.document doc = sb.build (IS); Element root = doc.getrootelement (); json.put (root.getName (), iterateLement (root)); Rückkehr JSON; } private static jsonObject iterateLement (Elementelement) {list node = element.getChildren (); Element et = null; JsonObject obj = new JsonObject (); Listliste = null; für (int i = 0; i <node.size (); i ++) {list = new LinkedList (); et = (Element) Knoten.get (i); if (etstexttrim (). Equals ("")) {if (etsgetChildren (). size () == 0) Fortsetzung; if (obj.containsKey (etsgetName ())) {list = (list) obj.get (etgname ()); } list.add (ITerateLement (et)); obj.put (etsgetName (), list); } else {if (obj.containsKey (etsgetName ())) {list = (list) obj.get (etsgetName ()); } list.add (etstetexttrim ()); obj.put (etsgetName (), list); }} return obj; } public static void main (String [] args) löst Jdomexception aus, ioException {String xml = "<? Xml Version =/" 1.0/"coding =/" 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> <NocifyUrl> http://mobaopay.com/callback.do </notifyUrl> <ArementSummary> order </tradesummary> </mobaoAccount> "; JsonObject json = xml2json (xml.getBytes ()); System.out.println (json.tojonstring ()); }}
Ausführungsergebnisse
XML Originaltext:
<xml version = "1.0" coding = "utf-8"?> <mobaoaccount messagetype = "Usermobilepay" platformID = "B2CTest"> <orderno> m20150521084825 </orderno> <userAmt> 5000.00 </tradaMt> </incips> </commission> </commission> </commission> </userId> </usw./usw./teadr. <Merchid> xiaolong1 </merchid> <tradetype> 0 </tradetype> <custparam> 123 </custParam> <WhilifyUrl> http://mobaopay.com/callback.do </notifyurl> <Atradesummary> order </tradesummern
Konvertierte JSON -Formatdaten
{ "MoBaoAccount": { "Commission": [ "0.5" ], "CustParam": [ "123" ], "MerchID": [ "zhuxiaolong1" ], "NotifyUrl": [ "http://mobaopay.com/callback.do" ], "OrderNo": [ "M20150521084825" ], "TradeAmt": [ "5000.00"], "tradesummary": ["Order"], "UserId": ["Zhuxiaolong"], "Tradetyp": ["0"]}}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.