xml添加多层解析

This commit is contained in:
egzosn
2019-12-02 23:20:43 +08:00
parent 31e1b2ea61
commit 16b26d4a2f
13 changed files with 77 additions and 17 deletions

24
.gitignore vendored Normal file
View File

@@ -0,0 +1,24 @@
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
.idea
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-ali</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

View File

@@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
@@ -232,10 +233,11 @@ 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) {
m.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()) {
m.put(node.getNodeName(), getChildren(nodeList));
} else if (node.getNodeType() == Node.ELEMENT_NODE ) {
m.put(node.getNodeName(), node.getTextContent());
}
}
} catch (Exception e) {
@@ -323,13 +325,47 @@ public class XML {
value = "";
}
org.w3c.dom.Element filed = document.createElement(entry.getKey());
if (value instanceof Map){
/* if (value instanceof Map){
map2Xml((Map)value, document, filed);
}else if (value instanceof List){
List vs = (List)value;
for (Object v : vs ){
if (value instanceof Map){
map2Xml((Map)value, document, filed);
}
}
map2Xml((Map)value, document, filed);
}else {
value = value.toString().trim();
filed.appendChild(document.createTextNode(value.toString()));
}
}*/
object2Xml(value, document, filed);
element.appendChild(filed);
}
}
private static void object2Xml(Object value, Document document, org.w3c.dom.Element element){
if (value instanceof Map){
map2Xml((Map)value, document, element);
}else if (value instanceof List){
List vs = (List)value;
for (Object v : vs ){
object2Xml(v, document, element);
}
// map2Xml((Map)value, document, element);
}else {
value = value.toString().trim();
element.appendChild(document.createTextNode(value.toString()));
}
}
public static void main(String[] args) {
String text = "<data><code>0</code><users><user><id>1</id><name>张三</name></user><user><id>2</id><name>张4</name></user></users></data>";
System.out.println( getMap2Xml(toJSONObject(text), "data", "utf-8"));
}
}

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-fuiou</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-payoneer</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-wx-youdian</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-java-wx</artifactId>

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>pay-java-parent</artifactId>
<groupId>com.egzosn</groupId>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -7,7 +7,7 @@
<groupId>com.egzosn</groupId>
<artifactId>pay-java-parent</artifactId>
<packaging>pom</packaging>
<version>2.12.9</version>
<version>2.13.1-SNAPSHOT</version>
<name>Pay Java - Parent</name>
<description>Pay Java Parent</description>
@@ -58,7 +58,7 @@
<properties>
<pay.version>2.12.9</pay.version>
<pay.version>2.13.1-SNAPSHOT</pay.version>
<httpmime.version>4.5.4</httpmime.version>
<log4j.version>1.2.17</log4j.version>
<fastjson.version>1.2.58</fastjson.version>