工具类--map 转成xml xml转成map
public class WxChatReq {
/**
* Map转换成XML
* @param data
* @return
* @throws Exception
*/
public static String recursionMapToXml(Map<String, String> data) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element root = document.createElement("xml");
document.appendChild(root);
for (String key: data.keySet()) {
String value = data.get(key);
if (value == null) {
value = "";
}
value = value.trim();
Element filed = document.createElement(key);
filed.appendChild(document.createTextNode(value));
root.appendChild(filed);
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
String output = writer.getBuffer().toString();
try {
writer.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
output = output.substring(output.indexOf("?>")+2,output.length());
return output;
}
/**
* XML 字符串转 Map
* @param xmlStr
* @return
*/
public static Map<String, String> xml2ToMap(String xmlStr) {
Map<String, String> map = new HashMap<String, String>();
if (isNullorEmpty(xmlStr)) {
throw new IllegalArgumentException("xml is empty");
} else {
Document document = null;
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(xmlStr.getBytes());
document =documentBuilder.parse(is);
} catch (ParserConfigurationException e) {
log.error(e.getMessage(), e);
}
} catch (SAXException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
Element element = document.getDocumentElement();
if (element != null) {
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
String nodeName = node.getNodeName();
String nodeText = node.getTextContent();
if("#text".equals(nodeName)){
continue;
}
map.put(nodeName, nodeText);
}
}
}
return map;
}
public static boolean isNullorEmpty(String xmlStr) {
if (null == xmlStr || "".equals(xmlStr)) {
return true;
} else {
return false;
}
}
public static String createSign(SortedMap<Object, Object> map, String key) {
StringBuffer sb = new StringBuffer();
for (Map.Entry<Object, Object> m : map.entrySet()) {
if ("".equals(m.getValue()) || null == m.getValue()) {
continue;
}
sb.append(m.getKey()).append("=").append(m.getValue()).append("&");
}
sb.append("key=" + key);
return MD5Utils.md5Encode(sb.toString(), "").toUpperCase();
}
}
工具类--map 转成xml xml转成map的更多相关文章
- 工具类Arrays.asList()方法把数组转换成集合
工具类Arrays.asList()方法把数组转换成集合 不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportedOperationException() 问 ...
- 工具类BitMap 把网络URL图片转换成BitMap
代码不复杂,直接把完整代码贴上. 这次是用到很旧的HttpURLConnection,那为什麽会用这个,因为我本来想转回okhttp的,可实在没时间转,项目就已经做下去了,结果转不回来. packag ...
- xml与java代码相互装换的工具类
这是一个java操作xml文件的工具类,最大的亮点在于能够通过工具类直接生成xml同样层次结构的java代码,也就是说,只要你定义好了xml的模板,就能一键生成java代码.省下了自己再使用工具类写代 ...
- java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合
Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
- 2015第30周三Spring常用工具类
文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...
- java学习-加载.properties工具类
javaWeb项目,要加载xxx.properties或其它如.txt, .md后缀的文本文件 文本内容有两种格式 key:value或者key=value 诸如Spring框架,Jfinal框架,都 ...
- Android工具类整合
Android-JSONUtil工具类 常用的Json工具类,包含Json转换成实体.实体转json字符串.list集合转换成json.数组转换成json public class JSONUtil ...
- Spring 常用的一些工具类
学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工 ...
- spring中常用工具类介绍
http://www.cnblogs.com/langtianya/p/3875103.html 文件资源操作 Spring 定义了一个 org.springframework.core.io ...
- spring常用的工具类
spring给我们提供了很多的工具类, 应该在我们的日常工作中很好的利用起来. 它可以大大的减轻我们的平时编写代码的长度. 因我们只想用spring的工具类, 而不想把一个大大的spring工程给引入 ...
随机推荐
- MySQL安装/卸载
http://jishu8.cc/2017/02/06/55/ 检查端口是否冲突:netstat nao | findstr 3307 启动服务:services.msc
- centos 7 下升级自带 sqlite3
问题 在 centos 7 上面运行 django 2.2 开发服务器时出现: django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or ...
- VTemplate模板引擎的使用--进阶篇
1.<vt:template>与<vt:include>标签的不同 <vt:template>和<vt:include> 标签都包含file属性,如果这 ...
- TTL 与 CMOS
Frm: https://blog.csdn.net/qq_27745395/article/details/76687175 http://baijiahao.baidu.com/s?id=1598 ...
- Cocos2d之运行Test项目
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 打开Test项目 路径为 C:\Dev\cocos2d-x-3.8.1\build 2.设定cpp-tests为启动项(当cpp ...
- jmeter 后台运行 setsid bin/jmeter -n -t .jmx文件 -l .jtl文件
备注: 另外,在Linux下我们有时候希望线程可以在后台运行,这样我们关闭当前连接后,线程依然可以运行,这里提供一个将 jmeter命令设置为后台线程的方法. 使用setsid命令: setsid ...
- if (user?.Identity?.IsAuthenticated ?? false)这几个问号分别都代表啥意思?
if (user?.Identity?.IsAuthenticated ?? false)这几个问号分别都代表啥意思? 0 悬赏园豆:5 [已解决问题] 浏览: 229次 解决于 2018-05-16 ...
- Linux安装配置Nginx服务器
如有需要可以加我Q群[308742428]大家一起讨论技术,有偿服务. 后面会不定时为大家更新文章,敬请期待. 喜欢的朋友可以关注下. 前言 今天搭建nginx服务器,来访问静态资源文件. Nginx ...
- shell只读变量
- CICS FILE OPEN
CEMT I CECD V FILE() GROUP() CEDA check error log in JESYSMSG FILE OPEN/CLOSE STATUS CICS ACTION res ...