xpath登陆案例
来源:互联网 发布:自然语言处理算法面试 编辑:程序博客网 时间:2024/06/05 00:33
xpath登陆案例:
用户登录功能:
用户输入用户名和密码 -> 到“数据库”查询是否有对应的用户 ->
有: 则表示登录成功
没有: 则表示登录失败
用xml当做数据库
user.xml 用来存储用户的数据
java代码:
package com.dom4j.xpath;import java.io.BufferedReader;import java.io.File;import java.io.InputStreamReader;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.SAXReader;public class Login {public static void main(String[] args) throws Exception {//获取输入字符流BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//登陆提示与接收System.out.println("请输入用户名:");String userName = br.readLine().trim();//去掉字符串两边空格,防止异常System.out.println("请输入密码:");String password = br.readLine().trim();//去掉字符串两边空格,防止异常//验证是否登陆成功login(userName, password);}private static void login(String userName, String password) throws Exception {//读取login.xml中的文件Document document = new SAXReader().read(new File("./src/login.xml"));/* * 方式1: *///查找name属性值为传入的userName的user标签String xpath = "//user[@userName='" + userName +"']";/*Element ele = (Element)document.selectSingleNode(xpath);if(ele != null){//假如用户名存在//读取所有name属性值为传入的userName的user标签中的属性名为password的属性值String pwd = ele.attributeValue("password");if(password.equals(pwd)){//如果密码也匹配System.out.println("恭喜【" + userName + "】登陆成功!!!");}else{System.out.println("对不起您的密码输入错误!!!");}}else{System.out.println("该用户名不存在!!!");}*//* * 方式2: *///查找userName属性值为“用户输入”,且password属性值为“用户输入”的user标签xpath = "//user[@userName='" + userName + "' and @password='" + password + "']";Node node = document.selectSingleNode(xpath);if(node != null){//查找到的user标签存在,则登陆成功System.out.println("恭喜【" + userName + "】登陆成功!!!");}else{System.out.println("登陆失败!!!");}}}
login.xml数据库:
<?xml version="1.0" encoding="UTF-8"?><users><user id="001" userName="木丁西" password="1234"></user><user id="002" userName="admin" password="admin"></user><user id="003" userName="刘先森" password="刘先森"></user></users>
效果:
0 0
- xpath登陆案例
- 登陆案例
- 登陆案例
- servlet 登陆注销案例
- Struts2登陆案例
- WebService的登陆案例
- springmvc登陆拦截案例
- wpf数据库连接登陆案例
- java-登陆注册案例
- Struts2登陆案例
- 糗事百科案例(使用 xpath模块)
- 案例:使用XPath的的爬虫
- Struts2入门之登陆案例
- 用户登陆注册案例分析
- Java 实现简单登陆案例
- Session(案例_登陆)
- spring登陆简单小案例
- Cookie用法案例-->自动登陆
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- A survey on still image based human action recognition
- 剑指Offer系列-面试题40:数组中只出现一次的数字
- C++中near与far的区别
- 【转】php获取日期及strtotime()的方法
- xpath登陆案例
- python超级简单爬虫
- Hibernate中的Criteria的投影和条件查询和排序和分页
- 【九度OJ】题目1173:查找 解题报告
- Hive常用的SQL命令操作
- bzoj 3239: Discrete Logging BSGS
- 【Codeforces 140 A. New Year Table】 + 精度
- 文章标题
- iOS 打包时出现在other items