XMLXPath.java
1.2 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
package com.tianbo.util.XML;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Node;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class XMLXPath {
public static String getSingleValueByPath(Document document,String path){
Node node = document.selectSingleNode(path);
if (node!=null){
String nodeValue = node.getStringValue();
return nodeValue;
}else {
return null;
}
}
public static String getSingleValueByNodePath(Node document,String path){
Node node = document.selectSingleNode(path);
if (node!=null){
String nodeValue = node.getStringValue();
return nodeValue;
}else {
return null;
}
}
public static List<Node> getPathValues(Document document,String path){
List<Node> nodes= document.selectNodes(path);
return nodes;
}
public static void getPathValues2(Document document,String path){
List list = document.selectNodes(path);
for (Iterator it = list.iterator(); it.hasNext();) {
Attribute attr = (Attribute) it.next();
//TODO
}
}
}