xiao_1991
xiao_1991
采纳率0%
2015-11-18 08:57 阅读 3.6k

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • bdmh bdmh 2015-11-18 08:59

    你组好把json编码(比如base64)后,存入节点,因为可能有一些特殊字符,捣乱

    点赞 评论 复制链接分享
  • a87b01c14 sanxiaochengyu 2015-11-18 08:59

    用第三方库文件吧
    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);
        }
    }
    
    
    
    
    点赞 评论 复制链接分享
  • sxzylq 白马翰如 2015-11-18 09:00

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

    点赞 评论 复制链接分享

相关推荐