1 package itcast.dom4j;
2
3 import java.io.File;
4
5 import org.dom4j.Document;
6 import org.dom4j.DocumentException;
7 import org.dom4j.Node;
8 import org.dom4j.io.SAXReader;
9
10 public class Demo1 {
11
12 /**
13 * @param args
14 * @throws Exception
15 */
16 public static void main(String[] args) throws Exception {
17
18 String username = "11";
19 String password = "123";
20
21 // 检测xml文档是否有匹配的用户名和密码
22 SAXReader reader = new SAXReader();
23 Document document = reader.read(new File("src/users.xml"));
24
25 Node node = document.selectSingleNode("//user[@username='"+username+"'and @password='"+password+"']");
26
27 if(node==null){
28 System.out.println("用户名或密码错误");
29 }else{
30 System.out.println("login success");
31 }
32 }
33
34 }