XML string to JSON string __JS

Source: Internet
Author: User
Tags string to json

XML string to JSON string there are many ways to do it online, here are two kinds: 1, using Org.json package

Jar Address: Http://mvnrepository.com/artifact/org.json/json

Sample code:

Import org.json.JSONException;
Import Org.json.JSONObject;
Import Org.json.XML;

public class Jsonutils {public
     static string xml2jsonstring (String xml) throws jsonexception{
     Jsonobject Xmljsonobj = Xml.tojsonobject (XML);
     return xmljsonobj.tostring ();
     }
2. Use Org.jdom package

Jar Address: http://www.jdom.org/

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 String * @return successfully returned JSO n-formatted string; failed back null/@SuppressWarnings ("unchecked") public static string Xml2json (String xml) {Jsonobject obj = NE
		W 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 XML file * @reTurn successfully back to JSON-formatted string; failed back null/@SuppressWarnings ("unchecked") public static string Xml2json (file file) {Jsonobjec
		T 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.util.Map instance/@Suppre
		Sswarnings ("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> +" & Lt Mapgroup id= ' Sheboygan ' > "+" <Map> "+" <Type>MapGuideddddddd</Type> "+" <singletile>tru E</singletile> "+" <Extension> "+" <ResourceId>ddd</ResourceId> "+" </Extension> "+" ;/map> "+" <Map> "+" <Type>ccc</Type> "+" <SingleTile>ggg</SingleTile> "+" <ex Tension> "+" <ResourceId>aaa</ResourceId> "+" </Extension> "+" </Map> "+" <extension/&G
			t; "+" </MapGroup> "+" <ddd> "+" 33333333 "+" </ddd> "+" <ddd> "+" 444 "+" </ddd> "	+ "</MapSet>")); }
}

Reference:

1) http://blog.csdn.net/lovesummerforever/article/details/26396465

2) http://blog.csdn.net/duguwuyi/article/details/51500852

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.