XML 转 fastJSON
- import java.util.List;
- import org.dom4j.Attribute;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- /**
- * xml工具类
- * @author sleep
- * @date 2016-09-13
- */
- public class XmlTool {
- /**
- * String 转 org.dom4j.Document
- * @param xml
- * @return
- * @throws DocumentException
- */
- public static Document strToDocument(String xml) throws DocumentException {
- return DocumentHelper.parseText(xml);
- }
- /**
- * org.dom4j.Document 转 com.alibaba.fastjson.JSONObject
- * @param xml
- * @return
- * @throws DocumentException
- */
- public static JSONObject documentToJSONObject(String xml) throws DocumentException {
- return elementToJSONObject(strToDocument(xml).getRootElement());
- }
- /**
- * org.dom4j.Element 转 com.alibaba.fastjson.JSONObject
- * @param node
- * @return
- */
- public static JSONObject elementToJSONObject(Element node) {
- JSONObject result = new JSONObject();
- // 当前节点的名称、文本内容和属性
- List<Attribute> listAttr = node.attributes();// 当前节点的所有属性的list
- for (Attribute attr : listAttr) {// 遍历当前节点的所有属性
- result.put(attr.getName(), attr.getValue());
- }
- // 递归遍历当前节点所有的子节点
- List<Element> listElement = node.elements();// 所有一级子节点的list
- if (!listElement.isEmpty()) {
- for (Element e : listElement) {// 遍历所有一级子节点
- if (e.attributes().isEmpty() && e.elements().isEmpty()) // 判断一级节点是否有属性和子节点
- result.put(e.getName(), e.getTextTrim());// 沒有则将当前节点作为上级节点的属性对待
- else {
- if (!result.containsKey(e.getName())) // 判断父节点是否存在该一级节点名称的属性
- result.put(e.getName(), new JSONArray());// 没有则创建
- ((JSONArray) result.get(e.getName())).add(elementToJSONObject(e));// 将该一级节点放入该节点名称的属性对应的值中
- }
- }
- }
- return result;
- }
- }
XML 转 fastJSON的更多相关文章
- Spring Boot返回json数据及完美使用FastJson解析Json数据
Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...
- Fastjson反序列化漏洞基础
Fastjson反序列化漏洞基础 FastJson是alibaba的一款开源JSON解析库,可用于将Java对象转换为其JSON表示形式,也可以用于将JSON字符串转换为等效的Java对象. 0x0 ...
- FastJson测试用例
基础测试 package com.ai; import com.ai.test.daily.Student; import com.alibaba.fastjson.JSON; import com. ...
- 【Spring】HttpMessageConverter的作用及替换
相信使用过Spring的开发人员都用过@RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服 ...
- SpringMVC(二) 参数绑定 与 JSON
参数绑定 http请求传输的参数都是String类型,但是Hanlder业务方法中的参数都是我们指定的数据类型,如int,Object等,所以需要处理参数的类型转换.此项工作不需要我们开发人员去完成, ...
- @RequestBody、@ResponseBody注解是如何将输入输出转换成json的
@RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服务器通过交换原始文本进行通信,而这里其 ...
- HttpMessageConverter那回事
相信使用过Spring的开发人员都用过@RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服 ...
- AgileBoot - 基于SpringBoot + Vue3的前后端快速开发脚手架
AgileBoot 仓库 后端地址:https://github.com/valarchie/AgileBoot-Back-End 技术栈:Springboot / Spring Security / ...
- Spring MVC-学习笔记(3)参数绑定注解、HttpMessageConverter<T>信息转换、jackson、fastjson、XML
1.参数绑定注解 1>@RequestParam: 用于将指定的请求参数赋值给方法中的指定参数.支持的属性: 2>@PathVariable:可以方便的获得URL中的动态参数,只支持一个属 ...
随机推荐
- linux 之基本命令学习总结
前言:从今天开始写这系列linux博客了(是学习刘遄老师的<linux就该这么学>),视频学习的资源可以在b站上找到:https://www.bilibili.com/video/av45 ...
- oracle merge 目标表以及源表存在重复列的问题(转)
SQL> select * from t_source; ...
- Glassfish DeploymentException: Error in linking security policy for
http://stackoverflow.com/questions/7322476/glassfish-deploymentexception-error-in-linking-security-p ...
- 14.插入数据、复制数据--SQL
一.插入完整的行 要求指定表名和插入到新行中的值 INSERT INTO Customers ', 'Toy Land', '123 Any Street', 'New York', 'NY', ', ...
- 升级到spring security5遇到的坑-密码存储格式
遇到的问题 将spring security oauth2(包括spring security)升级到最新,代码没有改动,运行项目没有报错,但是页面登陆时报错:There is no Password ...
- 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 ...
- 简单ui
UI继承 jQuery 简易使用特性,提供高度抽象接口,短期改善网站易用性. jquery UI 是一个建立在 jQuery JavaScript 库上的小部件和交互库,您可以使用它创建高度交互的 W ...
- NgStyle和NgIf控制HTML标签显示的区别
通常web开发者会选择将元素样式属性display设为none来隐藏目标元素.采用这种方式,这些元素虽然不可见却仍然保存在DOM中,这样带来的好处是,如果元素不久就需要再次显示,组件不需要重新被初始化 ...
- php 判断字符串中是否包含另一个字符串 strpos
strpos (PHP 4, PHP 5, PHP 7) strpos — 查找字符串首次出现的位置 说明 strpos ( string $haystack , $needle [, int $o ...
- Burpsuite Professional安装及使用教程
转自:https://www.jianshu.com/p/edbd68d7c341 1.先从吾爱破解论坛下载工具:https://down.52pojie.cn/Tools/Network_Analy ...