如何将一个json字符串,如:{"data":{"mc":"雷达数据","wcmst":"政策法规","cqgz":"每天多次间隔2小时","drzdxx":{"c":{"mc":"标题","zd":"doctitle","lx":"普通文本","wcmc":"doctitle","sfxstlb":"否","btzwbs":"标题","bygz":"主题规则","cqgz2":"政策法规"}}}},转换成xml字符串片段,最终插入到已有的xml文件中。
你组好把json编码(比如base64)后,存入节点,因为可能有一些特殊字符,捣乱
用第三方库文件吧
net.sf.json.JSONObject 可以去 findjar.com中查找
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
*
* <p>Title: JSON-XML转换工具</p>
* <p>desc:
* <p>Copyright: Copyright(c)Gb 2012</p>
* @author http://www.ij2ee.com
* @time 上午8:20:40
* @version 1.0
* @since
*/
public class XmlJSON {
private static final String STR_JSON = "{\"name\":\"Michael\",\"address\":{\"city\":\"Suzou\",\"street\":\" Changjiang Road \",\"postcode\":100025},\"blog\":\"http://www.ij2ee.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);
}
}
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
Title: JSON-XML转换工具
desc:
Copyright: Copyright(c)Gb 2012
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);
}
}