json数据和xml文件的相互转换

来源:互联网 发布:崩坏3矩阵buff大全 编辑:程序博客网 时间:2024/05/21 09:14

一、将xml文件转换成json数据:

1.用到的工具:dom4j

2.基本的思路:

      a.从xml的根元素下开始对每一个子节点进行深度遍历(递归遍历);

      b.用两个map分别来存储每一层标签转换成json的结果和每层相同标签的个数;

      c.每遍历完一层都将结果拼接成json字符串,返回给上层;

      d.最后得到整个xml文件的json数据

3.核心代码:

/** * 将xml文件转换成json字符串 * @param filePath xml文件路径 * @return * @throws Exception */public String xmlToJson(String filePath) throws Exception{Document document = getNode(filePath);Element root = document.getRootElement(); return tojson(root.elements());}/** * 将根元素下的元素转换成json字符串 * @param element * @return */public String tojson(List<Element> element){/** * 1.存储每以层的结果,key为标签的名字,value为每层标签解析后的结果 * 2.相同的标签会用逗号拼接,组成json数组 */Map<String, String> resultMap = new HashMap<String, String>();Map<String, Integer> countMap = new HashMap<String, Integer>();//解析一层中的所有标签 for(Element ele:element){  String resultJson = "{"; String labelName = ele.getName(); //如果标签中有文本,则对应到json中为:labelName + "Text:":value String text = ele.getTextTrim(); if (text.length()>0) { resultJson += labelName + "Text:" + text + "," ;} resultJson += attributesToJson(ele); String tojson = tojson(ele.elements()); if (tojson.length()>0&&resultJson.length()>1) { resultJson += ",";} resultJson += tojson + "}"; String result = resultMap.get(ele.getName()); //将相同的标签解析后的json字符串用逗号拼接起来 if(result!=null){ resultMap.put(labelName, result+","+resultJson); countMap.put(labelName, countMap.get(labelName) + 1); }else { resultMap.put(labelName, resultJson); countMap.put(labelName, 1);} } //将一层所有的标签json字符串拼接起来 String json = ""; for(Entry<String, String> entry:resultMap.entrySet()){ if (countMap.get(entry.getKey())>1) { json += "\"" + entry.getKey() + "\"" + ":[" + entry.getValue() + "]"; }else {json += "\"" + entry.getKey() + "\"" + ":" + entry.getValue() ; } json += ","; } if (json.length()>0) { return json.substring(0, json.length()-1);} return json;}/** * 将标签的属性值转换成json字符串 * @param element * @return */public String attributesToJson(Element element){String attrJson = "";List<Attribute> attributes = element.attributes();for(Attribute attr:attributes){attrJson += "," + "\"" + attr.getName() + "\"" + ":" + "\"" + attr.getValue() + "\"";}if (attrJson.length()>0) {return attrJson.substring(1);}return attrJson;}


二、将json字符串转换成xml文件

1.用到 的工具:dom4j和gson

2.思路:和将xml转换成json方法差不多,通过深度遍历,遍历一个json对象所有的属性,并创建相应的标签和属性

        3.核心代码:

/** * 将json字符串转换成xml * @param json json字符串 * @param parentElement xml根节点 * @throws Exception */public void jsonToXml(String json, Element parentElement) throws Exception{JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();toXml(jsonObject, parentElement, null);}/**将json字符串转换成xml * @param jsonElement 待解析json对象元素 * @param parentElement 上一层xml的dom对象 * @param name 属性名字(可以是标签名字,也可以是标签属性的名字) */public void toXml(JsonElement jsonElement, Element parentElement, String name){if (jsonElement instanceof JsonArray) {JsonArray sonJsonArray = (JsonArray)jsonElement;for (int i = 0; i < sonJsonArray.size(); i++) {JsonElement arrayElement = sonJsonArray.get(i);toXml(arrayElement, parentElement, name);}}else if(jsonElement instanceof JsonObject){JsonObject sonJsonObject = (JsonObject)jsonElement;Element currentElement = null;if (name!=null) {currentElement = DocumentHelper.createElement(name);}Set<Entry<String, JsonElement>> set = sonJsonObject.entrySet();for(Entry<String, JsonElement> s:set){toXml(s.getValue(), currentElement!=null?currentElement:parentElement, s.getKey());}if (currentElement!=null) {parentElement.add(currentElement);}}else {addAttribute(parentElement, name, jsonElement.getAsString());}}public void addAttribute(Element element, String name, String value){element.addAttribute(name, value);}




0 0