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中的动态参数,只支持一个属 ...
随机推荐
- 笔记-迎难而上之Java基础进阶3
统计字符串中每一个不同的字符 import java.util.*; //统计字符串每一个字符出现的字数 public class StringDemo{ public static void mai ...
- VMware workstation 14 安装 iOS虚拟机
https://03k.org/vmware-macos.html https://jingyan.baidu.com/article/363872ec206a356e4ba16f30.html 1. ...
- thinkphp5部署到LAMP服务器显示Access denied.
问题:thinkphp5部署到LAMP服务器,首页正常访问,其余页面访问显示Access denied 解决:1.先把文件夹权限改为777 2. 进入服务器,改文件.将php.ini的值改成1
- HDU-1150-MachineSchedule(二分图匹配)
链接:https://vjudge.net/problem/HDU-1150#author=0 题意: 在一个工厂,有两台机器A,B生产产品.A机器有n种工作模式(模式0,模式1....模式n-1). ...
- HTML——表单
总结: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- mysql in和exists性能比较和使用
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的两个表大小相当,那么用i ...
- Tensorflow版Faster RCNN源码解析(TFFRCNN) (3)推断(测试)过程使用RPN时代码运行流程
本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第三篇 推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu 原文见:https://hom ...
- C# 加密、解密函数
#region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name=" ...
- sql 容易被忽视的点
1 dual select查询语句只有select就可以,但为了规范,凑结构,可以加个dual 例:select now() from dual; 这个概念是Oracle中的.在mysql中可写可不写 ...
- ES6学习(1)
let 和 const 命令 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效.for循环的计数器,就很合适使用let命令. 下面的代码 ...