json和xml互转(用json-lib+dom4j方法)

json-lib是通过XMLSerializer的read和write方法进行xml的读取和json的写;xml转json时如果是元素的属性,会在json里的key前加一个@标识

1. 首先准备好jar包,以下是pom文件:


		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
		


		
		
			dom4j
			dom4j
			1.6.1
		

		
		
			xom
			xom
			1.2.5
		

2.java代码演示:


package com.lrq.demo;

import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.dom4j.*;


public class TestJson2Xml {
    public static void main(String[] args) throws DocumentException {
        String str = "{\"CargosubmatterList\":{\"Cargosubmatter\":[{\"itemkindcode\":\"005\",\"itemCode\":\"9996\",\"itemname\":\"特约标的\",\"itemddetail\":\"特约标的\",\"rate\":\"10.00\",\"currency\":\"CNY\",\"exchangerate\":\"1.00\",\"shortrateflag\":\"1\",\"shortrate\":\"100.00\",\"calamount\":\"1\",\"mainitem\":\"1\",\"itemkindname\":\"国内公路货物运输定额保险(B款)\",\"itemkindno\":\"1\",\"itemno\":\"1\",\"chgamount\":\"0.00\",\"chgpremium\":\"0.00\"},{\"itemkindcode\":\"006\",\"itemCode\":\"9996\",\"itemname\":\"特约标的\",\"itemddetail\":\"特约标的\",\"rate\":\"0.85\",\"currency\":\"CNY\",\"exchangerate\":\"1.00\",\"shortrateflag\":\"1\",\"shortrate\":\"100.00\",\"calamount\":\"1\",\"mainitem\":\"0\",\"itemkindname\":\"附加第三者责任保险\",\"itemkindno\":\"2\",\"itemno\":\"1\",\"chgamount\":\"0.00\",\"chgpremium\":\"0.00\"}]},\"PlusInfo\":{\"liabilitystartdate\":\"本保单保险责任起期详见保单条款约定\",\"shadowagentcode\":\"M00000003878\",\"shadowagentname\":\"广东广福宏宇保险经纪有限公司福建分公司\",\"shadowagenttype\":\"2102\"}}";
        JSONObject json = JSONObject.fromObject(str);
        String result = json2Xml(json,"xml");
       /* String str1 = result.replace("","   ");
        String str2 = str1.replace("","   ");*/
        System.out.println("json转xml:"+result);
        JSONObject xmlJson =  xml2Json(result);
        System.out.println("xml转json:"+xmlJson);
    }

    public static String json2Xml(JSONObject json, String rootName) throws DocumentException {
        String sXml = "";
        XMLSerializer xmlSerializer = new XMLSerializer();
        xmlSerializer.setTypeHintsEnabled(false);
        xmlSerializer.setRootName(rootName);
        String sContent = xmlSerializer.write(json,"utf-8");
        try {
            Document docCon = DocumentHelper.parseText(sContent);
            sXml = docCon.getRootElement().asXML();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return sXml;
    }

    public static JSONObject xml2Json(String xml){
        XMLSerializer xmlSerializer = new XMLSerializer();
        //将xml转为json(注:如果是元素的属性,会在json里的key前加一个@标识)
        String  json =  xmlSerializer.read(xml).toString();
       return JSONObject.fromObject(json);
    }

}


3. 控制台输出

如果是jsonArray会在前面加标签,但是解析的时候依然正常

json和xml互转(用json-lib+dom4j方法)_第1张图片








你可能感兴趣的:(【Java】)