Demand:
The server returns XML data and needs to parse the XML data to generate the JSON file
The importance of building your own resource pool
Required JAR Package: http://download.csdn.net/detail/zk673820543/9511502
Import Java.io.ByteArrayInputStream; Import Java.io.File; Import Java.io.InputStream; Import Java.util.HashMap; Import java.util.LinkedList; Import java.util.List; Import Java.util.Map; Import Net.sf.json.JSONObject; Import org.jdom.Document; Import org.jdom.Element; Import Org.jdom.input.SAXBuilder; public class Xml2jsonutil {/** * Converts an XML-formatted string into JSON format * * @param XML * XML-formatted string * @return successfully returned a JSON-formatted string; failed back null */@SuppressWarnings ("unchecked") public static string Xml2json (Strin g xml) {Jsonobject obj = new Jsonobject (); try {InputStream is = new Bytearrayinputstream (xml.getbytes ("Utf-8")); Saxbuilder sb = new Saxbuilder (); Document doc = Sb.build (IS); Element root = Doc.getrootelement (); Obj.put (Root.getname (), iterateelement (root)); return obj.tostring (); } catch (Exception e) {e.printstAcktrace (); return null; }}/** * Convert an XML-formatted string into JSON format * * @param file * Java.io.File instance is a valid XML file * @return successfully reversed back to the JSON-formatted string; null */@SuppressWarnings ("unchecked") public static string Xml2json (File F ile) {Jsonobject obj = new Jsonobject (); try {saxbuilder sb = new Saxbuilder (); Document doc = sb.build (file); Element root = Doc.getrootelement (); Obj.put (Root.getname (), iterateelement (root)); return obj.tostring (); } catch (Exception e) {e.printstacktrace (); return null; }}/** * An iterative method * * @param element *: Org.jdom.Element * @return java.u Til. Map instance */@SuppressWarnings ("unchecked") private static Map Iterateelement (element Element) {Lis T Jiedian = Element.getchildren (); Element et = Null Map obj = new HashMap (); List List = null; for (int i = 0; i < jiedian.size (); i++) {list = new LinkedList (); ET = (Element) jiedian.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; }//test public static void main (string[] args) {System.out.println (Xml2jsonutil.xml2json ("<maps Et> " + "<mapgroup id= ' Sheboygan ' >" + "<Map>" + "<Type>MapGuideddddddd</Type>" + "<SingleTile>true</SingleTile>" + "<Extension>" + "<RESOURCEID>DDD&L T;/resourceid> "+" </Extension> "+" </Map> "+" <Map> "+" <type>ccc</type> ; "+" <SingleTile>ggg</SingleTile> "+" <Extension> "+" <resourceid>aaa</resour Ceid> "+" </Extension> "+" </Map> "+" <extension/> "+" </MapGroup> " + "<ddd>" + "33333333" + "</ddd>" + "<ddd>" + "444" + "</ddd>" + "</ma Pset> ")); } }
The conversion here can only be converted to the content behind the label, the tag attributes are not able to be converted, the format is also messy, it is recommended to use Net.sf.json to convert, you can also use Oxstream to convert
Tools are human productivity.
Java XML to JSON format