xml格式转换成json格式

淩亂°似流年 2022-12-07 01:22 445阅读 0赞

一、xml格式转换成json格式

  1. package com.herocheer.bms.egjjfw.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.dom4j.*;
  6. import java.util.List;
  7. public class xmlAndJson {
  8. public static void main(String[] args) {
  9. try {
  10. String rest = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
  11. "\n" +
  12. "<ROOT>\n" +
  13. " <HEAD>\n" +
  14. " <DemoCode></DemoCode>\n" +
  15. " <ChanCode>11</ChanCode>\n" +
  16. " <DemoCent></DemoCent>\n" +
  17. " <DemoGlb></DemoGlb>\n" +
  18. " <DemoBank></DemoBank>\n" +
  19. " <DemoTell></DemoTell>\n" +
  20. " <Code>000</Code>\n" +
  21. " <Msg>交易成功</Msg>\n" +
  22. " <Date>20200909</Date>\n" +
  23. " <Time>093035</Time>\n" +
  24. " <TrsCode>6029</TrsCode>\n" +
  25. " <TrsChild></TrsChild>\n" +
  26. " <DemoSerial>1599615034960</DemoSerial>\n" +
  27. " </HEAD>\n" +
  28. " <BODY>\n" +
  29. " <total>500</total>\n" +
  30. " <pagesize>19</pagesize>\n" +
  31. " <rows>\n" +
  32. " <xh>70</xh>\n" +
  33. " <demo>1310000000</demo>\n" +
  34. " <company>阿里巴巴</company>\n" +
  35. " <zxjedhwe>C11112</zxjedhwe>\n" +
  36. " </rows>\n" +
  37. " <rows>\n" +
  38. " <xh>71</xh>\n" +
  39. " <demo>13110000000</demo>\n" +
  40. " <company>宇宙无敌天下商会</company>\n" +
  41. " <zxjedhwe>C11114</zxjedhwe>\n" +
  42. " </rows>\n" +
  43. " <rows>\n" +
  44. " <xh>72</xh>\n" +
  45. " <demo>140900000</demo>\n" +
  46. " <company>娃哈哈有限公司</company>\n" +
  47. " <zxjedhwe>C11111</zxjedhwe>\n" +
  48. " </rows>\n" +
  49. " </BODY>\n" +
  50. "</ROOT>";
  51. JSONObject jsonObject = xml2Json(rest);
  52. System.out.println(jsonObject);
  53. } catch (DocumentException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. /** * xml转json * @param xmlStr * @return * @throws DocumentException */
  58. public static JSONObject xml2Json(String xmlStr) throws DocumentException {
  59. Document doc = DocumentHelper.parseText(xmlStr);
  60. JSONObject json = new JSONObject();
  61. dom4j2Json(doc.getRootElement(), json);
  62. return json;
  63. }
  64. public static void dom4j2Json(Element element, JSONObject json) {
  65. // 如果是属性
  66. for (Object o : element.attributes()) {
  67. Attribute attr = (Attribute) o;
  68. if (StringUtils.isNotEmpty(attr.getValue())) {
  69. json.put("@" + attr.getName(), attr.getValue());
  70. }
  71. }
  72. List<Element> chdEl = element.elements();
  73. if (chdEl.isEmpty() && StringUtils.isNotEmpty(element.getText())) { // 如果没有子元素,只有一个值
  74. json.put(element.getName(), element.getText());
  75. }
  76. for (Element e : chdEl) { // 有子元素
  77. if (!e.elements().isEmpty()) { // 子元素也有子元素
  78. JSONObject chdjson = new JSONObject();
  79. dom4j2Json(e, chdjson);
  80. Object o = json.get(e.getName());
  81. if (o != null) {
  82. JSONArray jsona = null;
  83. if (o instanceof JSONObject) { // 如果此元素已存在,则转为jsonArray
  84. JSONObject jsono = (JSONObject) o;
  85. json.remove(e.getName());
  86. jsona = new JSONArray();
  87. jsona.add(jsono);
  88. jsona.add(chdjson);
  89. }
  90. if (o instanceof JSONArray) {
  91. jsona = (JSONArray) o;
  92. jsona.add(chdjson);
  93. }
  94. json.put(e.getName(), jsona);
  95. } else {
  96. if (!chdjson.isEmpty()) {
  97. json.put(e.getName(), chdjson);
  98. }
  99. }
  100. } else { // 子元素没有子元素
  101. for (Object o : element.attributes()) {
  102. Attribute attr = (Attribute) o;
  103. if (StringUtils.isNotEmpty(attr.getValue())) {
  104. json.put("@" + attr.getName(), attr.getValue());
  105. }
  106. }
  107. if (!e.getText().isEmpty()) {
  108. json.put(e.getName(), e.getText());
  109. }
  110. }
  111. }
  112. }
  113. }

控制台输出:

  1. { "HEAD":{ "Msg":"交易成功","ChanCode":"11","Time":"093035","TrsCode":"6029","DemoSerial":"1599615034960","Code":"000","Date":"20200909"},
  2. "BODY":{ "total":"500","pagesize":"19","rows":[{ "xh":"70","zxjedhwe":"C11112","company":"阿里巴巴","demo":"1310000000"},
  3. { "xh":"71","zxjedhwe":"C11114","company":"宇宙无敌天下商会","demo":"13110000000"},
  4. { "xh":"72","zxjedhwe":"C11111","company":"娃哈哈有限公司","demo":"140900000"}]}}

发表评论

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

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

相关阅读

    相关 XML格式JSON格式

    > 前言: > > XML和JSON是两种常见的数据交换格式,它们在现代软件开发中扮演着重要的角色。本文将介绍这两种格式的基本概念、特点以及它们的使用场景,以帮助更好地理解和

    相关 java XMLJSON格式

    需求: 服务器传回来XML数据,需要对XML数据进行解析,生成JSON文件 建立自己的资源库的重要性 所需要的jar包下载地址:http://download.

    相关 JSON格式转换问题

    最近在使用Echarts做图表统计,在使用json2.js的JSON.parse()方法将JSON字符串转为对象时无法正常的解析字符串。问题截图如下: js代码为: