XPath提取xml文档数据

 package itcast.dom4j;

 import java.io.File;

 import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader; public class Demo1 { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { String username = "11";
String password = "123"; // 检测xml文档是否有匹配的用户名和密码
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/users.xml")); Node node = document.selectSingleNode("//user[@username='"+username+"'and @password='"+password+"']"); if(node==null){
System.out.println("用户名或密码错误");
}else{
System.out.println("login success");
}
} }
 <?xml version="1.0" encoding="UTF-8"?>
<users>
<user id="1" username="aaa" password="123" email="aa@sina.com" />
<user id="2" username="bbb" password="123" email="aa@sina.com" />
</users>
上一篇:linux日常---2、lamp.sh安装lamp环境中的linux操作


下一篇:node.js render模板