Java XML to JSON format

Source: Internet
Author: User

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>" + "&LT;RESOURCEID&GT;DDD&L T;/resourceid> "+" </Extension> "+" </Map> "+" <Map> "+" <type>ccc</type&gt ; "+" <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.