java map转xml 工具类_工具类--map 转成xml xml转成map

public class WxChatReq {

/**

* Map转换成XML

* @param data

* @return

* @throws Exception

*/

public static String recursionMapToXml(Map 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 xml2ToMap(String xmlStr) {

Map map = new HashMap();

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 map, String key) {

StringBuffer sb = new StringBuffer();

for (Map.Entry 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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值