Tags: xml to json JSON2014-05-20 20:55 6568 People read Comments (6) favorite reports Classification:"J2SE Foundation" (a)
The code looks like this, and the importance of the codebase is found in this example, and if you build your own code base, you can get it out of your own code base, so you can take whatever you need to get out of it at any time. The codebase is guaranteed to be normative and correct, and has simple module annotations.
This is today on the Internet to find the XML to JSON format code, others packaged quickly, saving me a lot of time ah.
[Java]View PlainCopyprint?
- 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 a string in XML format to JSON format
- *
- * @param xml
- * String in XML format
- * @return successfully returned a JSON-formatted string;
- */
- @SuppressWarnings ("unchecked")
- public static string Xml2json (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;
- }
- }
- /**
- * Convert a string in XML format to JSON format
- *
- * @param file
- * Java.io.File instance is a valid XML file
- * @return successfully reversed back to JSON-formatted string;
- */
- @SuppressWarnings ("unchecked")
- public static String Xml2json (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 approach
- *
- * @param element
- *: Org.jdom.Element
- * @return Java.util.Map instances
- */
- @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= ' Sheboygan ' >" + "<Map> "
- + "<Type>MapGuideddddddd</Type>"
- + " <SingleTile>true</SingleTile>" + "<Extension>"
- + " <ResourceId>ddd</ResourceId>" + "</Extension>" + "</Map>"
- + " <Map>" + "<Type>ccc</Type>" + "<SingleTile>ggg</SingleTile>"
- + " <Extension>" + "<ResourceId>aaa</ResourceId>"
- + "</Extension>" + "</Map>" + "<extension/>" + "</MapGroup>"
- + "<ddd>" + "33333333" + "</ddd>" + "<ddd>" + "444" + "</ddd>"
- + "</MapSet>"));
- }
- }
Required jar package in my resource address: http://download.csdn.net/detail/lovesummerforever/7376755
http://blog.csdn.net/lovesummerforever/article/details/26396465
http://hanyi366.iteye.com/blog/1162365
-
Top
-
0
-
Step
-
0
Java XML to JSON format