首页 > 代码库 > json to xml
json to xml
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