1 Packagedemo; 2 ImportNet.sf.json.JSONObject;3 ImportNet.sf.json.xml.XMLSerializer;4 /** 5 * 6 * @authorLove Ru a WAN year7 * dom4j generate XML documents and parse XML documents8 */ 9 Public classXmltojson {Ten One Public Static voidMain (string[] args) { AString xml= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?><results_list><results><result> False</result><error> object references are not set to an instance of an object. </error></results></results_list> "; -String json=Xml2json (XML); -Jsonobject Object =Jsonobject.fromobject (JSON); theJsonobject results = Jsonobject.fromobject (object.getstring ("Results")); -String result=results.getstring ("Result"); -String error=results.getstring ("Error"); - System.out.println (results); + System.out.println (result); - System.out.println (error); + } A Public Staticstring Xml2json (String xml) { at return NewXMLSerializer (). Read (XML). toString (); - } -}
PS: This example converts a string in XML format, not XML, if the XML transformation can first convert XML to string and then use this example to implement
Java converts text of XML type to JSON