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的更多相关文章

  1. 工具类Arrays.asList()方法把数组转换成集合

    工具类Arrays.asList()方法把数组转换成集合 不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportedOperationException() 问 ...

  2. 工具类BitMap 把网络URL图片转换成BitMap

    代码不复杂,直接把完整代码贴上. 这次是用到很旧的HttpURLConnection,那为什麽会用这个,因为我本来想转回okhttp的,可实在没时间转,项目就已经做下去了,结果转不回来. packag ...

  3. xml与java代码相互装换的工具类

    这是一个java操作xml文件的工具类,最大的亮点在于能够通过工具类直接生成xml同样层次结构的java代码,也就是说,只要你定义好了xml的模板,就能一键生成java代码.省下了自己再使用工具类写代 ...

  4. java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合

    Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...

  5. 2015第30周三Spring常用工具类

    文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...

  6. java学习-加载.properties工具类

    javaWeb项目,要加载xxx.properties或其它如.txt, .md后缀的文本文件 文本内容有两种格式 key:value或者key=value 诸如Spring框架,Jfinal框架,都 ...

  7. Android工具类整合

    Android-JSONUtil工具类 常用的Json工具类,包含Json转换成实体.实体转json字符串.list集合转换成json.数组转换成json public class JSONUtil ...

  8. Spring 常用的一些工具类

    学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工 ...

  9. spring中常用工具类介绍

    http://www.cnblogs.com/langtianya/p/3875103.html 文件资源操作     Spring 定义了一个 org.springframework.core.io ...

  10. spring常用的工具类

    spring给我们提供了很多的工具类, 应该在我们的日常工作中很好的利用起来. 它可以大大的减轻我们的平时编写代码的长度. 因我们只想用spring的工具类, 而不想把一个大大的spring工程给引入 ...

随机推荐

  1. html{-webkit-text-size-adjust:none;}(取消浏览器最小字体限制)

    2016年10月13日 09:31:58 ITzhongzi 阅读数 9409   1.当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 ht ...

  2. webstorm 去点右边白线

    file>settings>editor>general>appearance>show right margin(configured in code style oo ...

  3. Jmeter beanshell断言 org.json.jar包下载

    链接:https://pan.baidu.com/s/1O01ODjlKyqmz2NyDT0MCww 提取码:a5va 欢迎关注微信公众号:软件测试汪,qq技术交流群:809111560

  4. upc组队赛2 Hakase and Nano【思维博弈】

    Hakase and Nano 题目描述 Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). ...

  5. Linux(四)—— 项目部署与ansible自动化部署

    目录 项目部署与ansible自动化部署 一.项目部署 二.ansible自动化部署(python自动化运维) 1.安装ansible 2.ansible例子 3.ansible自动化部署nginx ...

  6. Raspberry Pi 开机启动QT程序

    https://blog.csdn.net/coekjin/article/details/52498212 https://blog.csdn.net/dubuzherui/article/deta ...

  7. python之字符串中插入变量

    方法一:也是 比较好用的,功能教齐全 s="{name} is {sex}" print(s.format(name="zzy",sex="girl& ...

  8. transform:translate(-50%,-50%)

    和父亲元素没关系,走自己盒子宽度一半

  9. lombok 注解简单介绍

    一.Lombok 的简单介绍和使用 Lombok是一个可以帮助我们简化 Java 代码编写的工具类,通过采用注解的方式简化了 JavaBean 的编写,使我们写的类更加简洁. 1. 添加 Lombok ...

  10. 图像元数据编辑软件:MetaImage使用流程讲解

    MetaImage是唯一的macOS工具,允许在处理所有类型的标签格式时编辑,读取和写入元数据.在时尚的界面中导航,您可以更改图像元数据的所有内容,并对数百张相似的照片进行相同的编辑. https:/ ...