XPath
dom4j
快速定位
拥有强大的选择器
//利用xpath提取xml文档数据
SAXReader reader = new SaxReader();
Document document = reader.read("book.xml");
String value = document.selectSingleNode("//作者").getText();
System.out.println(value);
<?xml version="1.0" encoidng="UTF-8"?>
<!--xml文件user.xml-->
<users>
<user id="1" username="aa" password="123" email="aa@sina.com"/>
<user id="2" username="bb" password="123" email="bb@sina.com"/>
</users>
//查找xml文档中是否存在匹配的用户名与密码
public static void main(Stirng[] args){
String userNamae = "xxx";
Stirng passWord = "123";
//检测是否存在匹配的用户名与密码
SAXReader reader = new SAXReader();
Document documnet = reader.read(new File("user.xml"));
Node node = (Node)documnet.selectSingleNode("//user[@username='"+userName+"' and @password='"+passWord+"']");
if(node==null){
System.out.println("用户名或密码错误");
}else{
System.out.println("登陆成功");
}
}