標籤:net org class int getjson main XML instance csdn
package com.yile.test;
import org.json.XML;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
public class Xml2JsonTest {
private static String xml = "<root><user><name>weless</name><sex></sex></user></root>";
public static void main(String[] args) {
testOrgJSon();
testXmlSerializer();
}
public static void testOrgJSon(){
org.json.JSONObject jsonObj = XML.toJSONObject(xml);
System.out.println("org.json xml2json:"+jsonObj);
org.json.JSONObject user = jsonObj.getJSONObject("root").getJSONObject("user");//org.json需去除根標籤
System.out.println("使用者名稱:"+user.getString("name"));
System.out.println("性別:"+user.getString("sex"));
}
public static void testXmlSerializer(){
XMLSerializer xmlSerializer = new XMLSerializer();
JSONObject jsonObject = (JSONObject)xmlSerializer.read(xml);
System.out.println("json-lib xml2json result: "+jsonObject);
JSONObject user = jsonObject.getJSONObject("user");
System.out.println("使用者名稱:"+user.get("name"));
System.out.println("性別:"+(user.get("sex") instanceof String ? user.getString("sex"):""));
}
http://blog.csdn.net/lom9357bye/article/details/53291994
http://blog.csdn.net/lom9357bye/article/details/53291994
json to xml