引入maven依赖:

<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
public Map<String, Object> transferXml(Map<String, Object> result) {
String content = String.valueOf(result.get("ltc_content"));
Map<String, Object> data = new HashMap<>();
Document document = null;
try {
document = DocumentHelper.parseText(content);
Element root = document.getRootElement();
data = recursiveXml(root);
result.put("success", true);
result.put("msg", "处理成功");
result.put("data", data);
} catch (Exception e) {
e.printStackTrace();
result.put("success", false);
result.put("msg", "xml格式错误");
}
return result;
} public Map<String, Object> recursiveXml(Element element) throws Exception {
Map<String, Object> result = new HashMap<>();
String textTrim = element.getTextTrim();
result.put("text", textTrim);
//处理属性
Map<String, Object> attribute = new HashMap<>();
List<Attribute> attributeList = element.attributes();
for (Attribute attr : attributeList) {
attribute.put(attr.getName(), attr.getValue());
}
result.put("attribute", attribute);
//标签名
result.put("label", element.getName());
//子节点
List<Map<String, Object>> childrenList = new ArrayList<>();
List<Element> elementList = element.elements();
for (Element ele : elementList) {
Map<String, Object> child = recursiveXml(ele);
childrenList.add(child);
}
result.put("children", childrenList);
return result;
}

  

content为xml字符串数据。

dom4j 解析字符串成树形结构的更多相关文章

  1. javascript将平行的拥有上下级关系的数据转换成树形结构

    转换函数 var Littlehow = {}; /** * littlehow 2019-05-15 * 平行数据树形转换器 * @type {{format: tree.format, sort: ...

  2. c# List列表数据转换成树形结构

    把List列表结构 转换成树形结构 /// <summary> /// 构造树形Json /// </summary> public static class TreeJson ...

  3. 使用js将后台返回的数据转换成树形结构

    将类似如下数据转换成树形的数据: [ { id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1 ...

  4. 记一则 Lambda内递归调用方法将集合对象转换成树形结构

    public dynamic GetDepartments(string labID) { List<int> usedIDs = new List<int>(); //缓存已 ...

  5. 如何使用ABAP代码反序列化JSON字符串成ABAP结构

    假设我有这个JSON字符串如下图所示: 我的任务是解析出上图黑色方框里的几个字段,比如ObjectID, ETag, BuyerID, DateTime, ID, Name等等,把它们的值存储到对应A ...

  6. 【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题

    $.fn.extend({ //此方法解决了ie中jquery不识别非xml的类型的xml字符串的问题 tony tan findX: function (name) { if (this & ...

  7. Word排版成树形结构技巧

    初始文字 A A1 A2 B1 B1 B2 C C1 希望效果 关健设置

  8. Java数据封装成树形结构,多级

    参考地址:https://blog.csdn.net/chendu500qiang/article/details/91493147 1.实体类 @data public class PublishS ...

  9. idea目录结构子目录在父目录后面跟着改成树形结构

    1.点击项目窗口的设置按钮 2.取消Compact Middle Packages选项的对勾即可

随机推荐

  1. C# Selenium操作指南,关闭黑色CMD窗口/禁用图片/隐藏浏览器等

    引用部分:1. 2. 配置部分: ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(); dri ...

  2. 解决 No IDEA annotations attached to the JDK 1.8和xml文件没有代码提示

    Android studio3.3 用着用着突然xml里没有代码联想了,忙着做其他的就没管,写xml的时候就硬写... 然后今天用着突然在class文件上方提示No IDEA annotations ...

  3. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  4. CPU时序信号和分频

  5. Visual Studio + Qt:GetVarsFromMakefile任务意外失败

    问题: IntelliSense报告找不到头文件: 编译时报告GetVarsFromMakefile任务意外失败. 解决: 删除从Visual Studio装的Qt插件: 从Qt官网下载最新的插件:h ...

  6. 破解FTP登录密码的几种方法

    工具 Hydra X-Hydra Medusa Ncrack Patator Metasploit Hydra hydra -L /root/Desktop/user.txt -P /root/Des ...

  7. redis常用命令及持久化机制

    redis  常用命令 查找redis服务文件 find / -name  redis-server 查找配置文件 find / -name redis.conf 启动服务时候,要指定配置文件 启动r ...

  8. 23 SVN---版本控制系统

    1.SVN介绍 SVN是Subversion的简称,是一个自由开源的版本控制系统. Subversion将文件存放在中心版本库里,这个版本库很像一个普通的文件服务器,不同的是,它可以记录每一次文件和目 ...

  9. vs 远程调试

    程序在vs安装目录: D:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Remote Debugge ...

  10. Java多线程编程(3)--线程安全性

    一.线程安全性   一般而言,如果一个类在单线程环境下能够运作正常,并且在多线程环境下,在其使用方不必为其做任何改变的情况下也能运作正常,那么我们就称其是线程安全的.反之,如果一个类在单线程环境下运作 ...