我测试的环境是:WindowsXP、eclipse3.2、jdk1.6、dom4j1.6.1.jar、z-dom4j-1.4.jar
其中dom4j1.6.1.jar、z-dom4j-1.4.jar都可以在本人的资源库中进行下载,还有Dom4j的API,以及学习文档。
xml文件为:
<?xml version="1.0" encoding="GBK"?>
<root name="根节点">
<book name="书" address="北京">
<titlechild name="1" address="北京" author="马士兵">CS从入门到精通</titlechild>
<titlechild name="2" address="天津" author="a马士兵" price="37">aCS从入门到精通titlechild1</titlechild>
<titlechild name="3" address="南京" author="a马士兵" price="32">aCS从入门到精通titlechild2</titlechild>
<titlechild name="4" address="上海" author="title2马士兵" price="55">title2CS从入门到精通</titlechild>
<titlechild name="5" address="西安" author="title3马士兵" price="34">title3CS从入门到精通</titlechild>
</book>
</root>
测试代码为:
package org.dom4j.document_study_XPath;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Test_XPath {
public static void main(String[] args) {
readexml("C:\\2.xml");
}
//解析原始的xml
public static void readexml(String xmlpath){
SAXReader readxml = new SAXReader();
Document doc = null;
try {
doc = readxml.read(new File(xmlpath));//解析原始的xml
//TestSelectNodes_04(doc);
TestSelectNodes_05(doc);
} catch (Exception e) {
e.printStackTrace();
}
}
//通过节点的XPath路径获取document根路径下某个名称节点上第几个节点 document 一个Dom4j的Document对象
@SuppressWarnings("unchecked")
public static void TestSelectNodes_04(Document document) {
Node node =document.selectSingleNode("/root/book/titlechild[1]");
Node node1 =document.selectSingleNode("/root/book/titlechild[last()]");
Node node2 =document.selectSingleNode("/root/book/titlechild[last()-1]");
System.out.println("node第一个节点名称name=" +node.getName() + "\t节点属性name=" + node.valueOf("@name"));
System.out.println("node倒数第二个节点名称name=" +node2.getName() + "\t节点属性name=" + node2.valueOf("@name"));
System.out.println("node最后一个节点名称name=" +node1.getName() + "\t节点属性name=" + node1.valueOf("@name"));
List<Node> nodelist =document.selectNodes("/root/book/titlechild[position()<3]");
System.out.println("node的前两个节点名称");
for(Node node1_2 : nodelist){
System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性name=" + node1_2.valueOf("@name"));
}
}
//通过节点的XPath路径获取document根路径下某个名称,属性的节点 document 一个Dom4j的Document对象
@SuppressWarnings("unchecked")
public static void TestSelectNodes_05(Document document) {
System.out.println("获取所有名称为titlechild并且有属性:address的节点");
List<Node> nodelist =document.selectNodes("//titlechild[@address]");
for(Node node1_2 : nodelist){
System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性address=" + node1_2.valueOf("@address"));
}
System.out.println("获取所有名称为titlechild并且有属性address等于天津的节点");
List<Node> nodelist1 =document.selectNodes("//titlechild[@address='天津']");
for(Node node1_2 : nodelist1){
System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性address=" + node1_2.valueOf("@address"));
}
System.out.println("获取所有名称为titlechild并且有属性price的值大于35的节点");
List<Node> nodelist2 =document.selectNodes("//titlechild[@price>35]");
for(Node node1_2 : nodelist2){
System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性price=" + node1_2.valueOf("@price"));
}
System.out.println("获取所有名称为book并且有属性price的值小于35的节点");
List<Node> nodelist3 =document.selectNodes("//titlechild[@price<35]");
for(Node node1_2 : nodelist3){
System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性price=" + node1_2.valueOf("@price"));
}
}
}