1. import java.util.List;
  2.  
  3. import org.dom4j.Attribute;
  4. import org.dom4j.Document;
  5. import org.dom4j.DocumentException;
  6. import org.dom4j.DocumentHelper;
  7. import org.dom4j.Element;
  8.  
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11.  
  12. /**
  13. * xml工具类
  14. * @author sleep
  15. * @date 2016-09-13
  16. */
  17. public class XmlTool {
  18.  
  19. /**
  20. * String 转 org.dom4j.Document
  21. * @param xml
  22. * @return
  23. * @throws DocumentException
  24. */
  25. public static Document strToDocument(String xml) throws DocumentException {
  26. return DocumentHelper.parseText(xml);
  27. }
  28.  
  29. /**
  30. * org.dom4j.Document 转 com.alibaba.fastjson.JSONObject
  31. * @param xml
  32. * @return
  33. * @throws DocumentException
  34. */
  35. public static JSONObject documentToJSONObject(String xml) throws DocumentException {
  36. return elementToJSONObject(strToDocument(xml).getRootElement());
  37. }
  38.  
  39. /**
  40. * org.dom4j.Element 转 com.alibaba.fastjson.JSONObject
  41. * @param node
  42. * @return
  43. */
  44. public static JSONObject elementToJSONObject(Element node) {
  45. JSONObject result = new JSONObject();
  46. // 当前节点的名称、文本内容和属性
  47. List<Attribute> listAttr = node.attributes();// 当前节点的所有属性的list
  48. for (Attribute attr : listAttr) {// 遍历当前节点的所有属性
  49. result.put(attr.getName(), attr.getValue());
  50. }
  51. // 递归遍历当前节点所有的子节点
  52. List<Element> listElement = node.elements();// 所有一级子节点的list
  53. if (!listElement.isEmpty()) {
  54. for (Element e : listElement) {// 遍历所有一级子节点
  55. if (e.attributes().isEmpty() && e.elements().isEmpty()) // 判断一级节点是否有属性和子节点
  56. result.put(e.getName(), e.getTextTrim());// 沒有则将当前节点作为上级节点的属性对待
  57. else {
  58. if (!result.containsKey(e.getName())) // 判断父节点是否存在该一级节点名称的属性
  59. result.put(e.getName(), new JSONArray());// 没有则创建
  60. ((JSONArray) result.get(e.getName())).add(elementToJSONObject(e));// 将该一级节点放入该节点名称的属性对应的值中
  61. }
  62. }
  63. }
  64. return result;
  65. }
  66.  
  67. }

XML 转 fastJSON的更多相关文章

  1. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  2. Fastjson反序列化漏洞基础

    Fastjson反序列化漏洞基础 FastJson是alibaba的一款开源JSON解析库,可用于将Java对象转换为其JSON表示形式,也可以用于将JSON字符串转换为等效的Java对象. 0x0 ...

  3. FastJson测试用例

    基础测试 package com.ai; import com.ai.test.daily.Student; import com.alibaba.fastjson.JSON; import com. ...

  4. 【Spring】HttpMessageConverter的作用及替换

    相信使用过Spring的开发人员都用过@RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服 ...

  5. SpringMVC(二) 参数绑定 与 JSON

    参数绑定 http请求传输的参数都是String类型,但是Hanlder业务方法中的参数都是我们指定的数据类型,如int,Object等,所以需要处理参数的类型转换.此项工作不需要我们开发人员去完成, ...

  6. @RequestBody、@ResponseBody注解是如何将输入输出转换成json的

    @RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服务器通过交换原始文本进行通信,而这里其 ...

  7. HttpMessageConverter那回事

    相信使用过Spring的开发人员都用过@RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服 ...

  8. AgileBoot - 基于SpringBoot + Vue3的前后端快速开发脚手架

    AgileBoot 仓库 后端地址:https://github.com/valarchie/AgileBoot-Back-End 技术栈:Springboot / Spring Security / ...

  9. Spring MVC-学习笔记(3)参数绑定注解、HttpMessageConverter<T>信息转换、jackson、fastjson、XML

    1.参数绑定注解 1>@RequestParam: 用于将指定的请求参数赋值给方法中的指定参数.支持的属性: 2>@PathVariable:可以方便的获得URL中的动态参数,只支持一个属 ...

随机推荐

  1. linux 之基本命令学习总结

    前言:从今天开始写这系列linux博客了(是学习刘遄老师的<linux就该这么学>),视频学习的资源可以在b站上找到:https://www.bilibili.com/video/av45 ...

  2. oracle merge 目标表以及源表存在重复列的问题(转)

    SQL> select * from t_source;                                                                      ...

  3. Glassfish DeploymentException: Error in linking security policy for

    http://stackoverflow.com/questions/7322476/glassfish-deploymentexception-error-in-linking-security-p ...

  4. 14.插入数据、复制数据--SQL

    一.插入完整的行 要求指定表名和插入到新行中的值 INSERT INTO Customers ', 'Toy Land', '123 Any Street', 'New York', 'NY', ', ...

  5. 升级到spring security5遇到的坑-密码存储格式

    遇到的问题 将spring security oauth2(包括spring security)升级到最新,代码没有改动,运行项目没有报错,但是页面登陆时报错:There is no Password ...

  6. This file's format is not supported or you don't specify a correct format. 解决办法

    string path = @"c:\请假统计表.xlsx"; Workbook workBook = new Workbook(); workBook.Open(path); A ...

  7. 简单ui

    UI继承 jQuery 简易使用特性,提供高度抽象接口,短期改善网站易用性. jquery UI 是一个建立在 jQuery JavaScript 库上的小部件和交互库,您可以使用它创建高度交互的 W ...

  8. NgStyle和NgIf控制HTML标签显示的区别

    通常web开发者会选择将元素样式属性display设为none来隐藏目标元素.采用这种方式,这些元素虽然不可见却仍然保存在DOM中,这样带来的好处是,如果元素不久就需要再次显示,组件不需要重新被初始化 ...

  9. php 判断字符串中是否包含另一个字符串 strpos

    strpos (PHP 4, PHP 5, PHP 7) strpos — 查找字符串首次出现的位置 说明 strpos ( string $haystack ,  $needle [, int $o ...

  10. Burpsuite Professional安装及使用教程

    转自:https://www.jianshu.com/p/edbd68d7c341 1.先从吾爱破解论坛下载工具:https://down.52pojie.cn/Tools/Network_Analy ...