- import java.io.File;
-
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.Node;
- import org.dom4j.io.SAXReader;
- import org.junit.Test;
-
- public class Demo2 {
-
- @Test
- public void check() throws DocumentException{
- String username = "bbb";
- String password = "123";
-
- SAXReader reader = new SAXReader();
- Document document = reader.read(new File("src/user.xml"));
-
- Node node = document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");
- if(node==null){
- System.out.println("对不起,密码错误或者用户名不存在!");
- }
- else{
- System.out.println("欢迎你,"+username);
- }
- }
- }
复制代码- uesr.xml:
- [html] view plaincopy在CODE上查看代码片派生到我的代码片
- <?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="bb@sina.com" />
- </users>
复制代码
这里面的document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");是怎么匹配的?//user[@username又是什么意思?
|