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

àì夳堔傛蜴生んèń 2022-07-13 08:08 1023阅读 0赞

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

1.用到的工具:dom4j

2.基本的思路:

  1. a.从xml的根元素下开始对每一个子节点进行深度遍历(递归遍历);
  2. b.用两个map分别来存储每一层标签转换成json的结果和每层相同标签的个数;
  3. c.每遍历完一层都将结果拼接成json字符串,返回给上层;
  4. d.最后得到整个xml文件的json数据

3.核心代码:

  1. /**
  2. * 将xml文件转换成json字符串
  3. * @param filePath xml文件路径
  4. * @return
  5. * @throws Exception
  6. */
  7. public String xmlToJson(String filePath) throws Exception{
  8. Document document = getNode(filePath);
  9. Element root = document.getRootElement();
  10. return tojson(root.elements());
  11. }
  12. /**
  13. * 将根元素下的元素转换成json字符串
  14. * @param element
  15. * @return
  16. */
  17. public String tojson(List<Element> element){
  18. /**
  19. * 1.存储每以层的结果,key为标签的名字,value为每层标签解析后的结果
  20. * 2.相同的标签会用逗号拼接,组成json数组
  21. */
  22. Map<String, String> resultMap = new HashMap<String, String>();
  23. Map<String, Integer> countMap = new HashMap<String, Integer>();
  24. //解析一层中的所有标签
  25. for(Element ele:element){
  26. String resultJson = "{";
  27. String labelName = ele.getName();
  28. //如果标签中有文本,则对应到json中为:labelName + "Text:":value
  29. String text = ele.getTextTrim();
  30. if (text.length()>0) {
  31. resultJson += labelName + "Text:" + text + "," ;
  32. }
  33. resultJson += attributesToJson(ele);
  34. String tojson = tojson(ele.elements());
  35. if (tojson.length()>0&&resultJson.length()>1) {
  36. resultJson += ",";
  37. }
  38. resultJson += tojson + "}";
  39. String result = resultMap.get(ele.getName());
  40. //将相同的标签解析后的json字符串用逗号拼接起来
  41. if(result!=null){
  42. resultMap.put(labelName, result+","+resultJson);
  43. countMap.put(labelName, countMap.get(labelName) + 1);
  44. }else {
  45. resultMap.put(labelName, resultJson);
  46. countMap.put(labelName, 1);
  47. }
  48. }
  49. //将一层所有的标签json字符串拼接起来
  50. String json = "";
  51. for(Entry<String, String> entry:resultMap.entrySet()){
  52. if (countMap.get(entry.getKey())>1) {
  53. json += "\"" + entry.getKey() + "\"" + ":[" + entry.getValue() + "]";
  54. }else {
  55. json += "\"" + entry.getKey() + "\"" + ":" + entry.getValue() ;
  56. }
  57. json += ",";
  58. }
  59. if (json.length()>0) {
  60. return json.substring(0, json.length()-1);
  61. }
  62. return json;
  63. }
  64. /**
  65. * 将标签的属性值转换成json字符串
  66. * @param element
  67. * @return
  68. */
  69. public String attributesToJson(Element element){
  70. String attrJson = "";
  71. List<Attribute> attributes = element.attributes();
  72. for(Attribute attr:attributes){
  73. attrJson += "," + "\"" + attr.getName() + "\"" + ":" + "\"" + attr.getValue() + "\"";
  74. }
  75. if (attrJson.length()>0) {
  76. return attrJson.substring(1);
  77. }
  78. return attrJson;
  79. }

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

1.用到 的工具:dom4j和gson

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

  1. 3.核心代码:
  2. /**
  3. * 将json字符串转换成xml
  4. * @param json json字符串
  5. * @param parentElement xml根节点
  6. * @throws Exception
  7. */
  8. public void jsonToXml(String json, Element parentElement) throws Exception{
  9. JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
  10. toXml(jsonObject, parentElement, null);
  11. }
  12. /**将json字符串转换成xml
  13. * @param jsonElement 待解析json对象元素
  14. * @param parentElement 上一层xml的dom对象
  15. * @param name 属性名字(可以是标签名字,也可以是标签属性的名字)
  16. */
  17. public void toXml(JsonElement jsonElement, Element parentElement, String name){
  18. if (jsonElement instanceof JsonArray) {
  19. JsonArray sonJsonArray = (JsonArray)jsonElement;
  20. for (int i = 0; i < sonJsonArray.size(); i++) {
  21. JsonElement arrayElement = sonJsonArray.get(i);
  22. toXml(arrayElement, parentElement, name);
  23. }
  24. }else if(jsonElement instanceof JsonObject){
  25. JsonObject sonJsonObject = (JsonObject)jsonElement;
  26. Element currentElement = null;
  27. if (name!=null) {
  28. currentElement = DocumentHelper.createElement(name);
  29. }
  30. Set<Entry<String, JsonElement>> set = sonJsonObject.entrySet();
  31. for(Entry<String, JsonElement> s:set){
  32. toXml(s.getValue(), currentElement!=null?currentElement:parentElement, s.getKey());
  33. }
  34. if (currentElement!=null) {
  35. parentElement.add(currentElement);
  36. }
  37. }else {
  38. addAttribute(parentElement, name, jsonElement.getAsString());
  39. }
  40. }
  41. public void addAttribute(Element element, String name, String value){
  42. element.addAttribute(name, value);
  43. }

发表评论

表情:
评论列表 (有 0 条评论,1023人围观)

还没有评论,来说两句吧...

相关阅读