Home Web Front-end JS Tutorial Simple example of using XPath in dom4j

Simple example of using XPath in dom4j

May 23, 2018 pm 02:21 PM
dom4j xpath Example

Below I will bring you a simple example of using XPath in dom4j. Let me share it with you now and give it as a reference for everyone.

looks like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

package com.wzh.test.xpath;

  

import java.io.File;

  

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Node;

import org.dom4j.io.SAXReader;

  

public class Demo4 {

  

  public static void main(String[] args) throws DocumentException {

    SAXReader reader=new SAXReader();

    Document document=reader.read(new File("src/book.xml"));

      

    String value=document.selectSingleNode("//书名").getText();

    System.out.println(value);

      

      

    //检测xml文档是否有匹配的用户名和密码

    String username="aaa";

    String password="123";

    reader=new SAXReader();

    document=reader.read(new File("src/users.xml"));

    Node node=document.selectSingleNode("//user[@username='"+username+"'" +

        " and @password='"+password+"']");

    if(node==null)

    {

      System.out.println("用户名密码错误");

    }

    else

    {

      System.out.println("登录成功");

    }

  }

}

Copy after login

book.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

<?xml version="1.0" encoding="utf-8"?>

<书架>

   <书>

   <书名>Java就业培训教材</书名>

     <作者>张孝祥</作者>

     <售价>39.00元</售价>

   </书>

   <书>

   <书名>Java网页开发</书名>

     <作者>张孝祥</作者>

     <售价>29.00元</售价>

   </书>

</书架>

Copy after login

users. xml

1

2

3

4

5

<?xml version="1.0" encoding="UTF-8"?>

<users>

  <user id="1" username="aaa" password="123" email="aa@sina.com"></user>

  <user id="2" username="bbb" password="456" email="bb@sina.com"></user>

</users>

Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use native ajax to process json strings

##Introduction to the 4 common request methods of ajax in jQuery

Let’s talk about our views and understanding of Ajax form submission

The above is the detailed content of Simple example of using XPath in dom4j. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

SVM examples in Python SVM examples in Python Jun 11, 2023 pm 08:42 PM

Support Vector Machine (SVM) in Python is a powerful supervised learning algorithm that can be used to solve classification and regression problems. SVM performs well when dealing with high-dimensional data and non-linear problems, and is widely used in data mining, image classification, text classification, bioinformatics and other fields. In this article, we will introduce an example of using SVM for classification in Python. We will use the SVM model from the scikit-learn library

Learn best practice examples of pointer conversion in Golang Learn best practice examples of pointer conversion in Golang Feb 24, 2024 pm 03:51 PM

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

The relationship between the number of Oracle instances and database performance The relationship between the number of Oracle instances and database performance Mar 08, 2024 am 09:27 AM

The relationship between the number of Oracle instances and database performance Oracle database is one of the well-known relational database management systems in the industry and is widely used in enterprise-level data storage and management. In Oracle database, instance is a very important concept. Instance refers to the running environment of Oracle database in memory. Each instance has an independent memory structure and background process, which is used to process user requests and manage database operations. The number of instances has an important impact on the performance and stability of Oracle database.

VUE3 Getting Started Example: Making a Simple Video Player VUE3 Getting Started Example: Making a Simple Video Player Jun 15, 2023 pm 09:42 PM

As the new generation of front-end frameworks continues to emerge, VUE3 is loved as a fast, flexible, and easy-to-use front-end framework. Next, let's learn the basics of VUE3 and make a simple video player. 1. Install VUE3 First, we need to install VUE3 locally. Open the command line tool and execute the following command: npminstallvue@next Then, create a new HTML file and introduce VUE3: &lt;!doctypehtml&gt;

Verification code usage examples in Gin framework Verification code usage examples in Gin framework Jun 23, 2023 am 08:10 AM

With the popularity of the Internet, verification codes have become a necessary process for login, registration, password retrieval and other operations. In the Gin framework, implementing the verification code function has become extremely simple. This article will introduce how to use a third-party library to implement the verification code function in the Gin framework, and provide sample code for readers' reference. 1. Install dependent libraries Before using the verification code, we need to install a third-party library goCaptcha. To install goCaptcha, you can use the goget command: $goget-ugithub

VAE algorithm example in Python VAE algorithm example in Python Jun 11, 2023 pm 07:58 PM

VAE is a generative model, its full name is VariationalAutoencoder, which is translated into Chinese as variational autoencoder. It is an unsupervised learning algorithm that can be used to generate new data, such as images, audio, text, etc. Compared with ordinary autoencoders, VAEs are more flexible and powerful and can generate more complex and realistic data. Python is one of the most widely used programming languages ​​and one of the main tools for deep learning. In Python, there are many excellent machine learning and deep

PHP in action: Extracting data from XML documents using XPath PHP in action: Extracting data from XML documents using XPath Jun 13, 2023 pm 10:03 PM

XPath is a very useful tool when working with XML data using PHP. XPath is a language for locating elements in XML documents. It helps developers quickly and easily extract the required data from XML documents. In this article, we will introduce the basic concepts of XPath and explain in detail how to use XPath in PHP. We will demonstrate how to use XPath to extract data from an XML document and build a simple

GAN algorithm example in Python GAN algorithm example in Python Jun 10, 2023 am 09:53 AM

Generative Adversarial Networks (GAN) is a deep learning algorithm that uses two neural networks to compete with each other to generate new data. GAN is widely used for generation tasks in image, audio, text and other fields. In this article, we will use Python to write an example of a GAN algorithm for generating images of handwritten digits. Dataset Preparation We will use the MNIST data set as our training data set. The MNIST data set contains

See all articles