关于小程序退款使用JDOM解析XML时的问题
当需解析的xml中存在中文会出现字节流相关的报错。
/** * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。 * @param strxml * @return * @throws JDOMException * @throws IOException */ public static Map doXMLParse(String strxml) throws Exception { if (null == strxml || "".equals(strxml)) { return null; } Map m = new HashMap(); InputStream in = String2Inputstream(strxml); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(in); Element root = doc.getRootElement(); List list = root.getChildren(); Iterator it = list.iterator(); while (it.hasNext()) { Element e = (Element) it.next(); String k = e.getName(); String v = ""; List children = e.getChildren(); if (children.isEmpty()) { v = e.getTextNormalize(); } else { v = getChildrenText(children); } m.put(k, v); } // 关闭流 in.close(); return m; }
public static InputStream String2Inputstream(String str) {
return new ByteArrayInputStream(str.getBytes());
}
例如当商户余额不足时返回xml中有
<err_code_des><![CDATA[基本账户余额不足无法退款,请充值基本账户后再试]]></err_code_des>
解决:修改String转为InputStram的字节流编码问题
public static InputStream String2Inputstream(String str) throws UnsupportedEncodingException { return new ByteArrayInputStream(str.getBytes("utf-8")); }

浙公网安备 33010602011771号