xpath technology parses xml and cases simulate user login effects

黄舟
Release: 2017-02-16 15:26:29
Original
1671 people have browsed it

Problem: When using dom4j to query deep hierarchical structure nodes (labels, attributes, text), it is more troublesome! ! !

xpath is generated in this case-mainly used to quickly obtain the required [node object].


How to use xPath technology in dom4j


1) Import xPath supports jar package. jaxen-1.1-beta-6.jar

                                                                                                              Node> selectNodes("xpath expression"); Query multiple node objects

Node selectSingleNode("xpath expression"); Query a node object


xPath syntax

# Starting from the root position of xml or a child element (a hierarchy)

                                                                                                                                                                                                                                                               

                                                                                                                                                  because Elements under what conditions Conditions Relationship (equivalent on &&)

TEXT () Text represents selection text content

## This case

##                                                                                                             ’ s ’ s ’                                                           ‐ ‐ ‐ ‐   ‐ ‐ ‐ ‐ ‐ ​ ​ ​ ​ ​ ​ ​   1 gt;

Yes: Then it means that login is successful

没有: Then indicate the failure of login

## use as a database

User.xml to store users The data

code is as follows:


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

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * xpath案例: 模拟用户登录效果
 * @author APPle
 *
 */
public class Demo3 {

	public static void main(String[] args)throws Exception{
		//1.获取用户输入的用户名和密码
		BufferedReader br = 
				new BufferedReader(new InputStreamReader(System.in));//封装键盘录入,输入流
		
		System.out.println("请输入用户名:");
		String name = br.readLine();
		
		System.out.println("请输入密码:");
		String password = br.readLine();
		
		//2.到“数据库”中查询是否有对应的用户
		//对应的用户:  在user.xml文件中找到一个
		   //name属性值为‘用户输入’,且password属性值为‘用户输入’的user标签
		Document doc = new SAXReader().read(new File("./src/user.xml"));
		Element userElem = (Element)doc.selectSingleNode("//user[@name='" +name +"' and @password='"+password+"']");
		//在字符串中拼接变量的方法——先加一个双引号,再把光标移到双引号中间,写两个加号,再把光标移到加号中间写上变量。
		//System.out.println(userElem.getName());//查看当前节点对象内容
		if(userElem!=null){//说明在“数据库”里面找到了用户名和密码。
			//登录成功
			System.out.println("登录成功");
		}else{
			//登录失败
			System.out.println("登录失败");
		}
	}

}
Copy after login



The above is xpath technology parsing xml and case simulation user login Effect content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!






Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template