Xml—dom4j解析—简单的用户登录管理系统

users.xml

<?xml version="1.0" encoding="UTF-8"?>
<users>
<user username="aaa" password="111"/>
<user username="bbb" password="222"/>
<user username="ccc" password="333"/>
</users>

Xpath.java

package yyy.dom4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class Xpath {

private static boolean b;
private static int count = 1;
public static void main(String[] args) throws Exception {
login();
}

private static void login() throws IOException, DocumentException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入用户名:");
String username = br.readLine();
System.out.print("请输入密码:");
String password = br.readLine();

SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/users.xml"));
//利用Xpath技术获得匹配用户名username和密码password的节点
Node node = document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");
if(node==null)
b = false;
else
b = true;
if(b){
System.out.println("登陆成功!!");
return;
}
else{
System.out.println("账号或密码错误,请重新输入!!!");
login();
}
}

}

你可能感兴趣的:(dom4j)