XMLParse.java
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.example.demo.util.XML;
import ch.qos.logback.core.joran.spi.XMLUtil;
import com.example.demo.DemoApplication;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
public class XMLParse {
private static String xmlString;
private static String[] nodes;
private static Document document;
private static SAXBuilder builder;
private static Element root;
private static LinkedList<SubNode> listNode;
private static StringReader xmlReader;
private static InputSource xmlSource;
private String value;
public XMLParse() {
}
Logger logger = LoggerFactory.getLogger(DemoApplication.class);
/**
*
* @param xmlString xml字符窜
* @param xmlNodePath 节点参数(例如:/root/head/type)
*/
public XMLParse(String xmlString, String xmlNodePath) {
this.xmlString = xmlString;
this.nodes = xmlNodePath.split("/");
this.builder = new SAXBuilder();
this.root = null;
this.listNode = new LinkedList();
this.value = "";
this.document =new Document();
}
/**
* 取一对多关系的节点下的所有子节点的属性名 和 值
* @return
* @throws Exception
*/
public List<Map> getNodeValuesFromXmlString() throws Exception{
try {
this.xmlReader = new StringReader(this.xmlString);
this.xmlSource = new InputSource(this.xmlReader);
this.document = this.builder.build(xmlSource);
List<Map> maps = new ArrayList<Map>();
for(int i = 1; i < nodes.length; ++i) {
SubNode snode;
if (i >1) {
snode = (SubNode)listNode.getLast();
List<Element> eList = snode.getElement().getChildren(nodes[i]); //这里等于3的时候得到associatedPary节点 两个,下面应该循环这两个element,取他下面的所有children
if (eList != null && eList.size() > 0) {
SubNode s1 = new SubNode((Element)eList.get(0));
listNode.add(s1);
}
if(eList != null && eList.size() > 1 && i==nodes.length-1){ //只有进入的最尾节点的时候再循环去取同名下的一对多的ELEMENT所有子节点
for (Element element : eList){
List<Element> elements=element.getChildren();
Map<String,String> childMap = new HashMap<String, String>(); //用map存储每个值
for (Element childElements : elements){
String childName = childElements.getName();
String childValue = childElements.getText();
childMap.put(childName,childValue);
logger.debug(childValue);
}
maps.add(childMap);
}
}
} else if (1 == i) {
root = document.getRootElement();
snode = new SubNode(root);
listNode.add(snode);
}
}
return maps;
} catch (Exception var17) {
var17.printStackTrace();
throw var17;
} finally {
listNode.clear();
document = null;
root = null;
builder = null;
nodes = null;
}
}
/***
* 获取单一节点的值
* @return 单一节点值
* @throws Exception
*/
public final String getNodeValueFromXmlString() throws Exception {
try {
this.xmlReader = new StringReader(this.xmlString);
this.xmlSource = new InputSource(this.xmlReader);
this.document = this.builder.build(xmlSource);
for(int i = 1; i < nodes.length; ++i) {
SubNode snode;
if (i > 1) {
snode = (SubNode)listNode.getLast();
List<Element> eList = snode.getElement().getChildren(nodes[i]);
if (eList != null && eList.size() > 0) {
SubNode s1 = new SubNode((Element)eList.get(0));
listNode.add(s1);
}
} else if (1 == i) {
root = document.getRootElement();
snode = new SubNode(root);
listNode.add(snode);
}
}
SubNode n = (SubNode)listNode.getLast();
value = n.getElement().getText();
n = null;
return value;
} catch (Exception var17) {
var17.printStackTrace();
throw var17;
} finally {
listNode.clear();
document = null;
root = null;
builder = null;
nodes = null;
}
}
public static final String getNodeAttribute(String xmlFile, String xmlNodePath, String attributeName, String defaultValue) {
String[] nodes = xmlNodePath.split("/");
new Document();
SAXBuilder builder = new SAXBuilder();
Element root = null;
LinkedList<SubNode> listNode = new LinkedList();
String value = "";
Document document;
try {
document = builder.build(xmlFile);
for(int i = 1; i < nodes.length; ++i) {
SubNode snode;
if (i > 1) {
snode = (SubNode)listNode.getLast();
List<Element> eList = snode.getElement().getChildren(nodes[i]);
if (eList != null && eList.size() > 0) {
SubNode s1 = new SubNode((Element)eList.get(0));
listNode.add(s1);
}
} else if (1 == i) {
root = document.getRootElement();
snode = new SubNode(root);
listNode.add(snode);
}
}
SubNode n = (SubNode)listNode.getLast();
value = n.getElement().getAttributeValue(attributeName, defaultValue);
n = null;
} catch (JDOMException var18) {
var18.printStackTrace();
} catch (IOException var19) {
var19.printStackTrace();
} finally {
listNode.clear();
document = null;
root = null;
builder = null;
nodes = null;
}
System.out.println("key=" + xmlNodePath + " node attrivte value=" + value);
return value;
}
}