发布:2023/5/10 14:35:14作者:管理员 来源:本站 浏览次数:842
导入dom4j jar包
在pom.xml中的标签内加入要用到的jar包在仓库中的坐标
1.dom4j的jar包坐标
final String returnXML = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx8440b01badf5be52]]></appid><mch_id><![CDATA[1403452402]]></mch_id><nonce_str><![CDATA[VtgwoXiY23023w74]]></nonce_str><sign><![CDATA[39905078A04123151ED5F4399DAD042C]]></sign><result_code><![CDATA[SUCCESS]]></result_code><prepay_id><![CDATA[wx20170802095241a3dd4b58f60170434577]]></prepay_id><trade_type><![CDATA[JSAPI]]></trade_type></xml>";
Map<Object, Object> mapXML = new HashMap<Object, Object>();
Document doc;
try
{
doc = DocumentHelper.parseText(returnXML);
final Element el = doc.getRootElement();
mapXML = recGetXmlElementValue(el, mapXML);
LOG.info("========" + mapXML.size() + "+++" + mapXML.get("return_code"));
if (mapXML.get("return_code").equals("SUCCESS"))
{
//操作
}
}
catch (final DocumentException e)
{
e.printStackTrace();
return null;
}
private static Map<Object, Object> recGetXmlElementValue(final Element ele, final Map<Object, Object> map)
{
final List<Element> eleList = ele.elements();
if (eleList.size() == 0)
{
map.put(ele.getName(), ele.getTextTrim());
return map;
}
else
{
for (final Iterator<Element> iter = eleList.iterator(); iter.hasNext();)
{
final Element innerEle = iter.next();
recGetXmlElementValue(innerEle, map);
}
return map;
}
}