From 9d5a3aedea6afc4713448a8e5b564641a33a7536 Mon Sep 17 00:00:00 2001 From: egzosn Date: Tue, 24 Dec 2019 22:26:19 +0800 Subject: [PATCH] =?UTF-8?q?xml=20=E5=A4=9A=E5=B1=82=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/egzosn/pay/common/util/XML.java | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java b/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java index b61898b..a323e8a 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java @@ -142,12 +142,9 @@ public class XML { for (int idx = 0; idx < children.getLength(); ++idx) { Node node = children.item(idx); NodeList nodeList = node.getChildNodes(); - if (node.getNodeType() == Node.ELEMENT_NODE && nodeList.getLength() <= 1) { - if (null == json) { - json = new JSONObject(); - } - ((JSONObject) json).put(node.getNodeName(), node.getTextContent()); - } else if (node.getNodeType() == Node.ELEMENT_NODE && nodeList.getLength() > 1) { + int length = nodeList.getLength(); + + if (node.getNodeType() == Node.ELEMENT_NODE && length >= 1 && nodeList.item(0).hasChildNodes()) { if (null == json) { json = new JSONObject(); } @@ -167,6 +164,11 @@ public class XML { c.put(node.getNodeName(), getChildren(nodeList)); ((JSONArray) json).add(c); } + } else if (node.getNodeType() == Node.ELEMENT_NODE ) { + if (null == json) { + json = new JSONObject(); + } + ((JSONObject) json).put(node.getNodeName(), node.getTextContent()); } } @@ -199,20 +201,8 @@ public class XML { * @throws IOException xml io转化异常 */ public static T inputStream2Bean(InputStream in, Class clazz) throws IOException { - try { - - DocumentBuilder documentBuilder = newDocumentBuilder(); - org.w3c.dom.Document doc = documentBuilder.parse(in); - doc.getDocumentElement().normalize(); - NodeList children = doc.getDocumentElement().getChildNodes(); - JSON json = getChildren(children); - return json.toJavaObject(clazz); - } catch (Exception e) { - throw new PayErrorException(new PayException("XML failure", "XML解析失败\n" + e.getMessage())); - } finally { - in.close(); - } - + JSON json = toJSONObject(in); + return json.toJavaObject(clazz); } /** @@ -234,14 +224,13 @@ public class XML { Node node = children.item(idx); NodeList nodeList = node.getChildNodes(); int length = nodeList.getLength(); - if (node.getNodeType() == Node.ELEMENT_NODE && length >= 1 && nodeList.item(0).hasChildNodes()) { + if (node.getNodeType() == Node.ELEMENT_NODE && (length >1 || length==1 && nodeList.item(0).hasChildNodes())) { m.put(node.getNodeName(), getChildren(nodeList)); } else if (node.getNodeType() == Node.ELEMENT_NODE ) { m.put(node.getNodeName(), node.getTextContent()); } } } catch (Exception e) { -// e.printStackTrace(); throw new PayErrorException(new PayException("XML failure", "XML解析失败\n" + e.getMessage())); } finally { in.close(); @@ -364,8 +353,8 @@ public class XML { public static void main(String[] args) { - String text = "01张三2张4"; - System.out.println( getMap2Xml(toJSONObject(text), "data", "utf-8")); + String text = "01张三2张4"; + System.out.println( getMap2Xml(toJSONObject(text), "datas", "utf-8")); } }