Table of Contents
How to use xPath technology in dom4j
Home Backend Development XML/RSS Tutorial xpath technology parses xml and cases simulate user login effects

xpath technology parses xml and cases simulate user login effects

Feb 16, 2017 pm 03:26 PM

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!






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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

PHP development skills: How to implement user login restriction function PHP development skills: How to implement user login restriction function Sep 21, 2023 am 11:39 AM

PHP development skills: How to implement user login restriction function In website or application development, user login restriction function is a very important security measure. By limiting the number of login attempts and frequency of users, you can effectively prevent accounts from being maliciously cracked or brute force cracked. This article will introduce how to use PHP to implement user login restriction function and provide specific code examples. 1. Requirements analysis of user login restriction function User login restriction function usually includes the following requirements: Limitation on the number of login attempts: when the user continuously inputs errors

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

How to send SMS verification code and email notification when user logs in in PHP How to send SMS verification code and email notification when user logs in in PHP Sep 26, 2023 pm 08:40 PM

How to send SMS verification codes and email notifications when users log in in PHP. With the rapid development of the Internet, more and more applications require user login functions to ensure security and personalized experience. In addition to basic account and password verification, in order to improve user experience and security, many applications will also send mobile phone SMS verification codes and email notifications when users log in. This article will describe how to implement this functionality in PHP and provide corresponding code examples. 1. Send SMS verification code 1. First, you need someone who can send SMS

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

See all articles