可读性 JSON和XML的可读性可谓不相上下,XML略占上风。 可扩展性 XML天生有很好的扩展性,JSON当然也有,没有什么是XML能扩展,JSON不能的。 编码难度 XML有丰富的编码工具,比如Dom4j、JDom等,JSON也有json.org提供的工具,但是JSON的编码明显比XML容易许多,即使不借助工具也能写出JSON的代码,可是要写好XML就不太容易了。 解码难度 XML的解析得考虑子节点父节点关系,让人头昏眼花,而JSON的解析难度几乎为零。 流行度 XML已经被业界广泛的使用,而JSON才刚刚开始,但在Ajax领域,JSON凭借自身的优势有可能最终取代XML。
JSON转换为XML格式:DEMO:
需要jar包:json-lib-2.4-jdk15.jar,xom-1.1.jar,ezmorph-1.0.6.jar,commons-logging-1.1.1.jar,
commons-lang-2.5.jar,commons-collections-3.2.1.jar,commons-collections-3.1.jar,commons-beanutils-1.8.3.jar
package cn.song;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
public class Json2Xml {
/**
* @param args
*/
public static void main(String[] args) {
String json = "[{\"Product_ID\":\"0501010093\",\"Product_name\":\"美素(金装美素乐)奶粉二段900g\",\"Product_Pic\":\"jpg\",\"Product_URL\":\"http\",\"Product_price\":216},"
+ "{\"Product_ID\":\"0501010093\",\"Product_name\":\"美素(金装美素乐)奶粉二段900g\",\"Product_Pic\":\"jpg\",\"Product_URL\":\"http\",\"Product_price\":216},"
+ "{\"Product_ID\":\"0501010093\",\"Product_name\":\"美素(金装美素乐)奶粉二段900g\",\"Product_Pic\":\"jpg\",\"Product_URL\":\"http\",\"Product_price\":216},]";
JSONArray jsonObject = JSONArray.fromObject(json);
XMLSerializer xmlSerial = new XMLSerializer();
String xml = xmlSerial.write(jsonObject);
System.out.println(xml);
}
}