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();
    }
    }
  • 相关阅读:
    python ping监控
    MongoDB中一些命令
    进制转换(十进制转十六进制 十六进制转十进制)
    通过ssh建立点对点的隧道,实现两个子网通信
    linux环境下的各种后台执行
    python requests请求指定IP的域名
    不需要修改/etc/hosts,curl直接解析ip请求域名
    MongoDB数据update的坑
    windows平台使用Microsoft Visual C++ Compiler for Python 2.7编译python扩展
    rabbitmq问题之HTTP access denied: user 'guest'
  • 原文地址:https://www.cnblogs.com/tieandxiao/p/10931475.html
Copyright © 2011-2022 走看看