一、使用dom4j支持XPATH的操作
—可以直接獲取到某個元素,而不用一層一層的解析獲取
XPATH如何使用:
第一種形式:/AAA/BBB/CCC,一個/代表一層,表示獲取到AAA下面的BBB下面的CCC
第二種形式://BBB,表示和這個名稱相同的都可以得到,只要名稱是BBB都可以得到。//DDD/BBB:得到所有DDD下面的所有的BBB
第三種形式:/AAA/BBB/CCC/*,得到所有AAA下面BBB下面CCC下面的所有的元素。/*/*/*/BBB,表示限制前三層,前三層無論是什么名稱都可以得到其下面的所有的BBB。//*,得到所有的元素。
第四種形式:/AAA/BBB[1],AAA下面的第一個BBB。/AAA/BBB[last()],表示得到AAA下面的最后一個BBB
第五種形式://@id,表示只要標簽上有id屬性都可以得到,得到所有有id屬性的//BBB[@id],只要你BBB上有id屬性都可以得到,得到有id屬性的BBB
第六種形式://BBB[@id='b1'],得到含有屬性id且值為b1的所有BBB
二、使用xpath
默認情況下,dom4j不支持xpath。
要想支持需要導入jar包,jaxen-1.1-beta-6.jar
有兩個方法:selectNodes(“xpath標簽表達式”);獲得所有的元素,返回的是List,selectSingleNode(“xpath標簽表達式”);獲得一個元素,返回的是Node
1、查詢xml中的所有name元素的值
步驟:獲取document,使用方法selectNodes(“xpath標簽表達式”);
1
2
3
4
5
6
7
8
9
10
|
public static void Test1() throws Exception { Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH); List<Node> list = document.selectNodes( "//name" ); for (Node node : list) { //node是每一個元素具體的值 //得到每一個元素具體的值 String s = node.getText(); System.out.println(s); } } |
2、查詢xml中第一個name的值。步驟:先獲取document,然后構建xpath表達式。
1
2
3
4
5
6
7
|
public static void Test2() throws Exception{ Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH); Node name1 = document.selectSingleNode( "//p1[@id1='aaa']/name" ); //得到name的值 String s1 = name1.getText(); System.out.println(s1); } |
二、案例分析
添加、刪除、查詢
student.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "UTF-8" ?> < student > < stu > < id >01</ id > < name >zhangsan</ name > < age >20</ age > </ stu > < stu > < id >02</ id > < name >lisi</ name > < age >19</ age > </ stu > </ student > |
student.java
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
|
package cn.qing.ov; public class Student { private String id; private String name; private String age; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getAge() { return age; } public void setAge(String age) { this .age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]" ; } } |
stuService.java
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
|
package cn.qing.service; import java.io.FileOutputStream; import java.io.Writer; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import cn.qing.ov.Student; public class StuService { //增加 public static void addStu(Student student) throws Exception { SAXReader saxReader = new SAXReader(); Document document = saxReader.read( "src/student.xml" ); Element root = document.getRootElement(); //在根節點上添加stu Element stu = root.addElement( "stu" ); //在stu標簽上一次添加id,name,age Element id1 = stu.addElement( "id" ); Element name1 = stu.addElement( "name" ); Element age1 = stu.addElement( "age" ); //在id,name,age,上依次添加值 id1.setText(student.getId()); name1.setText(student.getName()); age1.setText(student.getAge()); //回寫到xml中 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter( new FileOutputStream( "src/student.xml" ),format); xmlWriter.write(document); xmlWriter.close(); } //刪除,根據學生ID進行刪除 /** * * @param id * @throws Exception * 1.創建解析器 * 2.獲得document * 3.獲取xml中的ID,使用xpath,返回一個list集合 * 4.遍歷list,判斷集合里的值是否和傳進來的id相同 * 5.如果相同,把id所在的stu刪除 * 6.回寫 */ public static void delStu(String id) throws Exception { SAXReader saxReader = new SAXReader(); Document document = saxReader.read( "src/student.xml" ); List<Node> list = document.selectNodes( "//id" ); for (Node node : list) { String idv = node.getText(); //判斷是否和傳遞的值相同 if (idv.equals(id)) { //得到stu的節點 Element stu = node.getParent(); //刪除是通過父節點進行刪除 Element student = stu.getParent(); student.remove(stu); } } OutputFormat format =OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter( new FileOutputStream( "src/student.xml" ),format); xmlWriter.write(document); xmlWriter.close(); } //查詢 public static Student selStu(String id) throws Exception { SAXReader saxReader = new SAXReader(); Document document = saxReader.read( "src/student.xml" ); List<Node> list = document.selectNodes( "//id" ); Student student = new Student(); for (Node node : list) { //node 是每一個id的值 String idv = node.getText(); if (idv.equals(id)) { Element stu = node.getParent(); String namev = stu.element( "name" ).getText(); String agev = stu.element( "age" ).getText(); student.setId(idv); student.setName(namev); student.setAge(agev); } } return student; } } |
測試Test.java
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
|
package cn.qing.test; import cn.qing.ov.Student; import cn.qing.service.StuService; public class Test { public static void main(String[] args) throws Exception { //testAdd(); //testDel(); testSel(); } //測試添加方法 public static void testAdd() throws Exception { //創建student對象 Student stu = new Student(); stu.setId( "03" ); stu.setName( "wangwu" ); stu.setAge( "18" ); StuService.addStu(stu); } //測試刪除方法 public static void testDel() throws Exception { StuService.delStu( "03" ); } //測試查詢方法 public static void testSel() throws Exception { Student student = StuService.selStu( "02" ); System.out.println(student.toString()); } } |
對于每一種類型,可以為其設置在不同的包,編程的思想
總結
以上就是本文關于java編程之xpath介紹的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/ilikejj0/article/details/78817541