Demand:
The server returns the 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 Download address: 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 {/** * Convert an XML-formatted string to JSON format * * @param XML * XML-formatted characters String * @return successfully returns JSON-formatted strings; failed back null/@SuppressWarnings ("unchecked") public static string xml2j
SON (String 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; }/** * Converts an XML-formatted string to JSON format * * @param file * Java.io.File instance is a valid XM L file * @return successfully back to JSON-formatted string; failed back null/@SuppressWarnings ("unchecked") public static string XML2
JSON (file file) {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 * @retur N Java.util.Map Instance */@SuppressWarnings ("unchecked") private static Map Iterateelement (element Element) {
List 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 ("<MapSet>" + "<mapgroup id= ' Sh") Eboygan ' > "+" <Map> "+" <Type>MapGuideddddddd</Type> "+" <sing Letile>true</singletile> "+" <Extension> "+" <ResourceId>ddd</ResourceId> "+ "</Extension>" + "</Map>" + "<Map>" + "<Type>ccc</Type>" + "<singletile
>ggg</SingleTile> "+" <Extension> "+" <ResourceId>aaa</ResourceId> " + "</Extension>" + "</Map>" + "<extension/>" + "</MapGroup>" + "<dd
D> "+" 33333333 "+" </ddd> "+" <ddd> "+" 444 "+" </ddd> "+" </MapSet> ");
}
}
The conversion here can only transform the content behind the tag, the label properties can not be converted, the format is also more chaotic, it is recommended to use Net.sf.json for conversion, you can use Oxstream to convert
Tools are human productivity.