xPath技术
1 引入
问题:当使用dom4j查询比较深的层次结构的节点(标签,属性,文本),比较麻烦!!!
2 xPath作用
主要是用于快速获取所需的节点对象。
3 在dom4j中如何使用xPath技术
1)导入xPath支持jar包 。 jaxen-1.1-beta-6.jar
2)使用xpath方法
List<Node> selectNodes("xpath表达式"); 查询多个节点对象
Node selectSingleNode("xpath表达式"); 查询一个节点对象
4 xPath语法
/ 绝对路径 表示从xml的根位置开始或子元素(一个层次结构)
// 相对路径 表示不分任何层次结构的选择元素。
* 通配符 表示匹配所有元素
[] 条件 表示选择什么条件下的元素
@ 属性 表示选择属性节点
and 关系 表示条件的与关系(等价于&&)
text() 文本 表示选择文本内容
代码练习:
package com.dom4j.xpath; import java.io.File;
import java.util.List; import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader; public class xpathExercise { public static void main(String[] args) throws Exception {
Document doc = new SAXReader().read(new File("./src/contact.xml"));
String xpath = "";
xpath = "/contactList";
xpath = "//contact/name";
xpath = "//name";
xpath = "/*/*/age";
xpath = "/contactList/*";
xpath = "/contactList//*";
xpath = "//contact[@value]";
xpath = "//contact[@id]";
xpath = "//contact[2]";
xpath = "//contact[last()]";
xpath = "//@id";
xpath = "//contact[not(@id)]";
xpath = "//contact[@id='003' and @value='hello']";
xpath = "//name/text()";//返回ext
xpath = "//contact/name[text()='张三']";
List<Node> list = doc.selectNodes(xpath);
for(Node node : list){
System.out.println(node);
}
} }
5 案例
用户登录功能:
用户输入用户名和密码 -> 到“数据库”查询是否有对应的用户 ->
有: 则表示登录成功
没有: 则表示登录失败
用xml当做数据库
user.xml 用来存储用户的数据
练习代码:
package com.xpath.example; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader; public class userLogin {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String user = reader.readLine(); System.out.println("请输入密码: ");
String pwd = reader.readLine(); Document doc = new SAXReader().read("./src/user.xml");
/*
String xpath = "/users/user";
List<Node> list = doc.selectNodes(xpath);
String id, name, passwd;
for(Node node : list){
Element auser = (Element)node;
id = auser.attribute("id").getValue();
name = auser.attribute("name").getValue();
passwd = auser.attribute("password").getValue();
if(name.equals(user) && passwd.equals(pwd)){
System.out.println(id + "在数据库中");
}
}
*/
Element userElement = (Element)doc.selectSingleNode("//user[@name='" + user +"' and @password='" + pwd +"']");
if(userElement == null){
System.out.println("登录失败");
}else{
System.out.println("登录成功");
} } }
练习:读取一个html中的用户信息
person.html:
<html>
<head>
<title>传智播客1月18号班通讯录</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<center><h1>12月16号就业班通讯录</h1></center>
<table border="1" align="center" id="contactForm">
<thead>
<tr><th>编号</th><th>姓名</th><th>性别</th><th>年龄</th><th>地址</th><th>电话</th></tr>
</thead>
<tbody>
<tr>
<td>001</td>
<td>张三</td>
<td>男</td>
<td>18</td>
<td>广州市天河区</td>
<td>134000000000</td>
</tr>
<tr>
<td>002</td>
<td>李四</td>
<td>女</td>
<td>20</td>
<td>广州市越秀区</td>
<td>13888888888</td>
</tr>
<tr>
<td>002</td>
<td>郭靖</td>
<td>男</td>
<td>30</td>
<td>广州市番禺区</td>
<td>1342214321</td>
</tr>
</tbody>
</table>
</body>
</html>
person类:
package com.xpath.example; public class Person {
private String id;
private String name;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
private String sex;
private String addr;
private String phone;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Person \n id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex
+ ", addr=" + addr + ", phone=" + phone + "\n";
} }
读取html写入person的主类:
package com.xpath.example; import java.io.File;
import java.util.List; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class read_html { public static void main(String[] args) throws Exception {
Document doc = new SAXReader().read(new File("./src/personList.html"));
List nodes = doc.selectNodes("//tr");
// System.out.println(nodes);
int num = nodes.size();
Person[] persons = new Person[num];
Element trNode;
for(int i = 1; i < num; i++){
persons[i] = new Person();
trNode = (Element)nodes.get(i);
//System.out.println(trNode.selectSingleNode("td[1]"));
persons[i].setId(trNode.selectSingleNode("td[1]").getText());
persons[i].setName(((Element)trNode.elements().get(1)).getText());
persons[i].setSex(((Element)trNode.elements().get(2)).getText());
persons[i].setAge(((Element)trNode.elements().get(3)).getText());
persons[i].setAddr(((Element)trNode.elements().get(4)).getText());
persons[i].setPhone(((Element)trNode.elements().get(5)).getText());
System.out.println(persons[i]);
}
} }