package com;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
* JSON-XML转换工具
*/
public class XmlJSON {
private static final String STR_JSON = "{\"name\":\"Tom\",\"address\":{\"city\":\"Suzou\",\"street\":\" Guanqian Road \",\"postcode\":000000},\"blog\":\"http://www.hydong.com\"}";
public static String xml2JSON(String xml) {
return new XMLSerializer().read(xml).toString();
}
public static String json2XML(String json) {
JSONObject jobj = JSONObject.fromObject(json);
String xml = new XMLSerializer().write(jobj);
return xml;
}
public static void main(String[] args) {
String xml = json2XML(STR_JSON);
System.out.println("xml = " + xml);
String json = xml2JSON(xml);
System.out.println("json=" + json);
}
}