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();
    }
    }
  • 相关阅读:
    【bzoj1029】[JSOI2007]建筑抢修
    【bzoj2748】[HAOI2012]音量调节
    【bzoj1041】[HAOI2008]圆上的整点
    【bzoj2743】[HEOI2012]采花
    【bzoj1034】[ZJOI2008]泡泡堂BNB
    【bzoj1260】[CQOI2007]涂色paint
    【bzoj2728】[HNOI2012]与非
    【bzoj2003】[Hnoi2010]Matrix 矩阵
    【bzoj1028】[JSOI2007]麻将
    利用adb截屏
  • 原文地址:https://www.cnblogs.com/tieandxiao/p/10931475.html
Copyright © 2011-2022 走看看