json字符串转换成xml字符串,最终存入xml文件中

如何将一个json字符串,如:{"data":{"mc":"雷达数据","wcmst":"政策法规","cqgz":"每天多次间隔2小时","drzdxx":{"c":{"mc":"标题","zd":"doctitle","lx":"普通文本","wcmc":"doctitle","sfxstlb":"否","btzwbs":"标题","bygz":"主题规则","cqgz2":"政策法规"}}}},转换成xml字符串片段,最终插入到已有的xml文件中。图片说明

3个回答

你组好把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

    • @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);
    }
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐