|
1 | 1 | package com.github.binarywang.wxpay.bean.result; |
2 | 2 |
|
| 3 | +import com.google.common.base.Joiner; |
3 | 4 | import com.google.common.collect.Maps; |
4 | 5 | import com.thoughtworks.xstream.XStream; |
5 | 6 | import com.thoughtworks.xstream.annotations.XStreamAlias; |
6 | | -import io.restassured.internal.path.xml.NodeChildrenImpl; |
7 | | -import io.restassured.internal.path.xml.NodeImpl; |
8 | | -import io.restassured.path.xml.XmlPath; |
9 | | -import io.restassured.path.xml.element.Node; |
10 | | -import io.restassured.path.xml.exception.XmlPathException; |
11 | 7 | import me.chanjar.weixin.common.util.ToStringUtils; |
12 | 8 | import me.chanjar.weixin.common.util.xml.XStreamInitializer; |
| 9 | +import org.apache.commons.lang3.StringUtils; |
13 | 10 | import org.slf4j.Logger; |
14 | 11 | import org.slf4j.LoggerFactory; |
15 | | - |
| 12 | +import org.w3c.dom.Document; |
| 13 | +import org.w3c.dom.NodeList; |
| 14 | +import org.xml.sax.SAXException; |
| 15 | + |
| 16 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 17 | +import javax.xml.parsers.ParserConfigurationException; |
| 18 | +import javax.xml.xpath.XPathConstants; |
| 19 | +import javax.xml.xpath.XPathExpressionException; |
| 20 | +import javax.xml.xpath.XPathFactory; |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
16 | 23 | import java.math.BigDecimal; |
17 | | -import java.util.Iterator; |
18 | 24 | import java.util.Map; |
19 | 25 |
|
20 | 26 | /** |
@@ -82,6 +88,11 @@ public abstract class WxPayBaseResult { |
82 | 88 | @XStreamAlias("sign") |
83 | 89 | private String sign; |
84 | 90 |
|
| 91 | + /** |
| 92 | + * xml的Document对象,用于解析xml文本 |
| 93 | + */ |
| 94 | + private Document xmlDoc; |
| 95 | + |
85 | 96 | /** |
86 | 97 | * 将单位分转换成单位圆 |
87 | 98 | * |
@@ -211,51 +222,74 @@ public void setSubMchId(String subMchId) { |
211 | 222 | * 将bean通过保存的xml字符串转换成map |
212 | 223 | */ |
213 | 224 | public Map<String, String> toMap() { |
214 | | - Map<String, String> result = Maps.newHashMap(); |
215 | | - XmlPath xmlPath = new XmlPath(this.xmlString); |
216 | | - NodeImpl rootNode = null; |
217 | | - try { |
218 | | - rootNode = xmlPath.get("xml"); |
219 | | - } catch (XmlPathException e) { |
| 225 | + if (StringUtils.isBlank(this.xmlString)) { |
220 | 226 | throw new RuntimeException("xml数据有问题,请核实!"); |
221 | 227 | } |
222 | 228 |
|
223 | | - if (rootNode == null) { |
224 | | - throw new RuntimeException("xml数据有问题,请核实!"); |
225 | | - } |
| 229 | + Map<String, String> result = Maps.newHashMap(); |
| 230 | + Document doc = this.getXmlDoc(); |
226 | 231 |
|
227 | | - Iterator<Node> iterator = rootNode.children().nodeIterator(); |
228 | | - while (iterator.hasNext()) { |
229 | | - Node node = iterator.next(); |
230 | | - result.put(node.name(), node.value()); |
| 232 | + try { |
| 233 | + NodeList list = (NodeList) XPathFactory.newInstance().newXPath() |
| 234 | + .compile("/xml/*") |
| 235 | + .evaluate(doc, XPathConstants.NODESET); |
| 236 | + int len = list.getLength(); |
| 237 | + for (int i = 0; i < len; i++) { |
| 238 | + result.put(list.item(i).getNodeName(), list.item(i).getTextContent()); |
| 239 | + } |
| 240 | + } catch (XPathExpressionException e) { |
| 241 | + throw new RuntimeException("非法的xml文本内容:" + xmlString); |
231 | 242 | } |
232 | 243 |
|
233 | 244 | return result; |
234 | 245 | } |
235 | 246 |
|
236 | | - private String getXmlValue(XmlPath xmlPath, String path) { |
237 | | - if (xmlPath.get(path) instanceof NodeChildrenImpl) { |
238 | | - if (((NodeChildrenImpl) xmlPath.get(path)).size() == 0) { |
239 | | - return null; |
240 | | - } |
| 247 | + /** |
| 248 | + * 将xml字符串转换成Document对象,以便读取其元素值 |
| 249 | + */ |
| 250 | + protected Document getXmlDoc() { |
| 251 | + if (this.xmlDoc != null) { |
| 252 | + return this.xmlDoc; |
| 253 | + } |
| 254 | + |
| 255 | + try { |
| 256 | + this.xmlDoc = DocumentBuilderFactory |
| 257 | + .newInstance() |
| 258 | + .newDocumentBuilder() |
| 259 | + .parse(new ByteArrayInputStream(this.xmlString.getBytes("UTF-8"))); |
| 260 | + return xmlDoc; |
| 261 | + } catch (SAXException | IOException | ParserConfigurationException e) { |
| 262 | + throw new RuntimeException("非法的xml文本内容:" + this.xmlString); |
241 | 263 | } |
242 | 264 |
|
243 | | - return xmlPath.getString(path); |
244 | 265 | } |
245 | 266 |
|
246 | | - protected <T> T getXmlValue(XmlPath xmlPath, String path, Class<T> clz) { |
247 | | - String value = this.getXmlValue(xmlPath, path); |
248 | | - if (value == null) { |
249 | | - return null; |
| 267 | + /** |
| 268 | + * 获取xml中元素的值 |
| 269 | + */ |
| 270 | + protected String getXmlValue(String... path) { |
| 271 | + Document doc = this.getXmlDoc(); |
| 272 | + String expression = String.format("/%s//text()", Joiner.on("/").join(path)); |
| 273 | + try { |
| 274 | + return (String) XPathFactory |
| 275 | + .newInstance() |
| 276 | + .newXPath() |
| 277 | + .compile(expression) |
| 278 | + .evaluate(doc, XPathConstants.STRING); |
| 279 | + } catch (XPathExpressionException e) { |
| 280 | + throw new RuntimeException("未找到相应路径的文本:" + expression); |
250 | 281 | } |
| 282 | + } |
251 | 283 |
|
252 | | - switch (clz.getSimpleName()) { |
253 | | - case "String": |
254 | | - return (T) value; |
255 | | - case "Integer": |
256 | | - return (T) Integer.valueOf(value); |
| 284 | + /** |
| 285 | + * 获取xml中元素的值,作为int值返回 |
| 286 | + */ |
| 287 | + protected Integer getXmlValueAsInt(String... path) { |
| 288 | + String result = this.getXmlValue(path); |
| 289 | + if (StringUtils.isBlank(result)) { |
| 290 | + return null; |
257 | 291 | } |
258 | 292 |
|
259 | | - throw new UnsupportedOperationException("暂时不支持此种类型的数据"); |
| 293 | + return Integer.valueOf(result); |
260 | 294 | } |
261 | 295 | } |
0 commit comments