[工具]Java xml 转 Json
2026/6/6 15:49:17 网站建设 项目流程

[工具]Java xml 转 Json

依赖

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.37</version> </dependency>

代码

import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.XmlUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import lombok.extern.slf4j.Slf4j; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @Slf4j public class WTool_Xml { public static JSONObject xmlToJson(String xml) { if(StrUtil.isBlank(xml)){ return null; } // long l = System.currentTimeMillis(); // JSONObject entries = WTool.xmlToJson(xml); // long l2 = System.currentTimeMillis(); // // log.info("用时:{}", l2 - l); // if(MapUtil.isNotEmpty(entries)){ // return entries; // } for (int i = 0; i < 2; i++) { try { // l = System.currentTimeMillis(); Document document = XmlUtil.parseXml(xml); // String nodeName = document.getNodeName(); // String textContent = document.getTextContent(); NodeList childNodes = document.getChildNodes(); JSONObject obj = JSONUtil.createObj(); digui(obj, childNodes); // l2 = System.currentTimeMillis(); // log.info("自定义转换用时:{}", l2 - l); return obj; }catch (Exception e){ e.printStackTrace(); xml = xml.replaceAll("\\\\", ""); } } return null; } public static JSONObject digui(JSONObject j节点Json, NodeList childNodes){ if(childNodes == null || childNodes.getLength() == 0){ return null; } // WTool_Xml_Entity j节点Json = WTool_Xml_Entity.builder().build(); // JSONObject j节点Json = JSONUtil.createObj(); int length = childNodes.getLength(); for (int i = 0; i < length; i++) { Node item = childNodes.item(i); // WTool_Xml_Entity z子节点Json = WTool_Xml_Entity.builder().build(); JSONObject z子节点Json = JSONUtil.createObj(); String j节点名称 = item.getNodeName(); if(j节点名称.equals("#text")){ // String textContent = item.getTextContent(); // z子节点Json.set("#text", textContent); // j节点Json.set(j节点名称, z子节点Json); continue; } // System.out.println("====================" + j节点名称 + "========================"); //属性 NamedNodeMap attributes = item.getAttributes(); if(attributes != null && attributes.getLength() > 0){ JSONObject s属性Json = JSONUtil.createObj(); // JSONObject j节点属性Json = z子节点Json.getJ节点属性Json(); for (int j = 0; j < attributes.getLength(); j++) { Node attr = attributes.item(j); // System.out.println("属性:" + attr.getNodeName() + "=" + attr.getNodeValue()); s属性Json.set(attr.getNodeName(), attr.getNodeValue()); // z子节点Json.set(attr.getNodeName(), attr.getNodeValue()); // j节点属性Json.set(attr.getNodeName(), attr.getNodeValue()); } z子节点Json.set("#属性", s属性Json); } //子集 NodeList z子集 = item.getChildNodes(); if(z子集.getLength() <= 0){ //内容 String textContent = item.getTextContent(); if(!StrUtil.isBlank(textContent)){ z子节点Json.set("#text", textContent); // z子节点Json.setJ节点内容(textContent); // System.out.println("内容:" + textContent); } }else if(z子集.getLength() == 1){ Node z子集1 = z子集.item(0); String z子集1节点名称 = z子集1.getNodeName(); if(z子集1节点名称.equals("#cdata-section") || z子集1节点名称.equals("#text")){ //内容 String textContent = z子集1.getTextContent(); if(!StrUtil.isBlank(textContent)){ z子节点Json.set("#text", textContent); // z子节点Json.setJ节点内容(textContent); // System.out.println("内容:" + textContent); } }else{ digui(z子节点Json, z子集); } }else{ digui(z子节点Json, z子集); } // z子节点Json.set("#子集合", z子集合); if (j节点Json.containsKey(j节点名称)) { //变为集合 Object s上一个相同的节点 = j节点Json.get(j节点名称); if(s上一个相同的节点 instanceof JSONArray){ ((JSONArray) s上一个相同的节点).add(z子节点Json); }else{ JSONArray x相同节点Json集合 = new JSONArray(); x相同节点Json集合.add(s上一个相同的节点); x相同节点Json集合.add(z子节点Json); j节点Json.set(j节点名称, x相同节点Json集合); } }else{ j节点Json.set(j节点名称, z子节点Json); } } return j节点Json; } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询