当前位置: 首页 > ds >正文

[工具]Java xml 转 Json

[工具]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.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) {//		JSONObject entries = WTool.xmlToJson(xml);
//		System.out.println(entries);Document document = XmlUtil.parseXml(xml);//		String nodeName = document.getNodeName();
//		String textContent = document.getTextContent();NodeList childNodes = document.getChildNodes();JSONObject obj = JSONUtil.createObj();digui(obj, childNodes);return obj;}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")){continue;}
//			System.out.println("====================" + j节点名称 + "========================");//内容String textContent = item.getTextContent();if(!StrUtil.isBlank(textContent)){z子节点Json.set("#text", textContent);
//				z子节点Json.setJ节点内容(textContent);
//				System.out.println("内容:" + textContent);}//属性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 childNodes1 = item.getChildNodes();digui(z子节点Json, childNodes1);
//			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;}}

http://www.xdnf.cn/news/126.html

相关文章:

  • 开源 vs. 闭源:大模型的未来竞争格局
  • mysql中优先使用datetime存储时间
  • ES中常用的Query和查询作用,以及SpringBoot使用实例
  • 人工智能工程师的职业前景如何?
  • 数据结构-八大排序
  • 《Spring Boot 测试框架指南:@SpringBootTest与Mockito的最佳实践》
  • 江湖路远,唯PUT可稳:Express 路由更新招式全解
  • AI 组件库是什么?如何影响UI的开发?
  • 选择 iOS 按键精灵无根有根越狱辅助工具的理由
  • Git 进阶之路:高效协作之分支管理
  • 2025妈妈杯数学建模D题完整分析论文(共42页)(含模型建立、代码)
  • 设计模式 --- 外观模式
  • srp batch
  • langchain tools源码解析以及扩展
  • 单线服务器有什么优点
  • 【Linux学习笔记】进程调度与切换之O(1)调度算法
  • 【c语言】深入理解指针3——回调函数
  • OpenCV day6
  • 数据结构学习笔记 :树与二叉树详解
  • 学习threejs,使用EffectComposer后期处理组合器(采用RenderPass、GlitchPass渲染通道)
  • PyCharm入门导览
  • 蓝桥杯之前缀和
  • 基于单片机的温湿度采集系统(论文+源码)
  • 好数对的数目
  • 【系统分析师】-软件工程
  • 2025mathorcup妈妈杯数学建模挑战赛B 题:音智策引迁程,老城焕新颜,思路,模型,代码,持续更新中
  • 【文件操作与IO】详细解析文件操作与IO (一)
  • 15 nginx 中默认的 proxy_buffering 导致基于 http 的流式响应存在 buffer, 以 4kb 一批次返回
  • VUE3多国语言切换(国际化)
  • 数据结构初阶:二叉树(二)