zoukankan      html  css  js  c++  java
  • 工具类--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();
    }
    }
  • 相关阅读:
    JAVA字符串格式化-String.format()的使用
    分布式文件系统MFS(moosefs)实现存储共享(第二版)
    iOS 动画学习
    复制对象(一)copy和mutableCopy方法
    iOS-获取当前网页的 url 和 title 和 html
    排序算法 c实现
    常用正则表达式
    iOS-获取的NSDate date时间与实际相差8个小时解决方案
    网络爬虫基本原理(二)
    网络爬虫基本原理(一)
  • 原文地址:https://www.cnblogs.com/tieandxiao/p/10931475.html
Copyright © 2011-2022 走看看