dom4j 常用操作
package com.wanbang.wbyyb.common.util; import com.alibaba.fastjson.JSONObject;
import com.wanbang.wbyyb.common.constant.ErrorEnum;
import com.wanbang.wbyyb.common.exception.BusinessException;
import com.wanbang.wbyyb.service.payment.impl.PaymentServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils; import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap; public class XmlUtils {
private static final Logger logger = LoggerFactory.getLogger(PaymentServiceImpl.class); /**
* 根据xml标签名的ASCII码正序排列的xml
* @param filePath
* @return
*/
public static Document getXml(String filePath){
Document document = null;
try{
Resource resource = null;
if (!StringUtils.isEmpty(filePath))
resource = new ClassPathResource(filePath);
else
throw new BusinessException(ErrorEnum.ERROR_PARAM); File file = resource.getFile(); /**
* 读取xml转有序map
*/
SAXReader reader = new SAXReader();
Document doc = reader.read(file);
Element root = doc.getRootElement();
Iterator iterator = root.elementIterator();
Map<String,Object> map = new TreeMap<>();
while (iterator.hasNext()) {
Element element = (Element) iterator.next();
String key = element.getName();
String value = element.getText();
map.put(key, value);
} /**
* 有序map转xml
*/
document = DocumentHelper.createDocument();
Element newRoot = document.addElement("xml");
Set<String> keys = map.keySet();
for(String k:keys) {
newRoot.addElement(k).addText(map.get(k).toString());
}
} catch (IOException e) {
logger.error("com.wanbang.wbyyb.service.payment.impl: ",e);
} catch (DocumentException e) {
logger.error("com.wanbang.wbyyb.service.payment.impl:",e);
}
return document;
}public static Object xml2Json(String xmlStr){
JSONObject jsonObject = new JSONObject();
try {
Document document = DocumentHelper.parseText(xmlStr);
Element element = document.getRootElement();
Iterator iterator = element.elementIterator();
while (iterator.hasNext()) {
Element ele = (Element)iterator.next();
jsonObject.put(ele.getName(), ele.getText());
}
} catch (DocumentException e) {
e.printStackTrace();
}
return jsonObject;
}
}
@Test
public void createTest() {
Document doc = DocumentHelper.createDocument();
//创建一个名为xml的根节点,因为是第一个创建,所以是根节点,再通过doc创建一个则会报错。
Element root = doc.addElement("xml");
Element codeEle = root.addElement("return_code");
Element msg = root.addElement("return_msg");
codeEle.addCDATA("SUCCESS"); //生成结果<return_code><![CDATA[SUCCESS]]></return_code>
codeEle.addAttribute("id", "101"); //添加属性
msg.setText("SUCCESS"); //生成结果<return_code>SUCCESS</return_code>
msg.setText("<![CDATA[SUCCESS]]>"); //生成结果<return_msg><![CDATA[SUCCESS]]></return_msg>
System.out.println(doc.asXML()); //xml转String
}
dom4j 常用操作的更多相关文章
- 【三】用Markdown写blog的常用操作
本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...
- php模拟数据库常用操作效果
test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...
- Mac OS X常用操作入门指南
前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右) =鼠标左键 control+按下 ...
- mysql常用操作语句
mysql常用操作语句 1.mysql -u root -p 2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...
- nodejs配置及cmd常用操作
一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...
- Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理
摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■ 详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...
- python 异常处理、文件常用操作
异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
- Linux Shell数组常用操作详解
Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...
随机推荐
- 整理一下 通知传值 Block传值
Block: 一. (1) 在需要传值的界面定义属性 // 点击collectionViewCell的回调 @property (nonatomic, copy) void(^Didcollectio ...
- GYM 101673E(暴搜预处理)
1.不会超过500个不同的串-- 2.样例没给has到has是怎么样的,实测是true. 3.记忆化别剪错枝就好,嘤嘤嘤-- const int maxn = 505 + 5; int n, m, t ...
- python之错题巩固
.#把班级学⽣数学考试成绩录⼊到⼀个列表中: 并求平均值. 要求: 录⼊的时候 # 要带着⼈名录⼊, 例如: 张三_44 li = [] : str_input = input('请输入你的姓名和分数 ...
- 安卓新的联网方式 Volley的使用(一)加载图片与 json
最近刚接触安卓, 以前搞wp ,一对比起来 ,安卓怎么这么麻烦.联网必须要重新开一个线程才可以.而且加载网络图片也很麻烦...花了很久一直卡在快速滑动加载网络图片的listview上面 ,一直很纠结痛 ...
- File "<stdin>" , line 1
写了一个hello.py,仅有一句,print 'hello world', 运行 Python hello.py 出错,提示: File "<stdin>" , li ...
- Objective-C Operators and Expressions
What is an Expression? The most basic expression consists of an operator, two operands and an assign ...
- CSS布局之-高度自适应
何为高度自适应? 高度自适应就是高度能跟随浏览器窗口的大小改变而改变,典型的运用在一些后台界面中上面一栏高度固定用作菜单栏或导航栏,下面一栏高度自适应用于显示内容.高度自适应不像宽度自适应那样简单,在 ...
- 重置Cacti密码
Cacti登录密码忘记,重置Cacti密码 用root用户进入系统 [root@localhsot]# mysql -u root -p mysql> show databases; mysql ...
- HTML iframe框架
iframe 作用: 就是在一个网页插入一个小窗口 窗口里面也是一个网页 <a href="http://www.baidu.com" target="da1& ...
- 0x00000124蓝屏问题解决方法
windows7-32位系统: 0x00000124蓝屏是系统问题,win7才有的, xp系统没有 . 解决办法:下载win7蓝屏补丁包解压安装就ok了. 说明:win7蓝屏补丁KB(25286140 ...