Home > Web Front-end > JS Tutorial > body text

Detailed explanation of using XPath in dom4j (with code)

php中世界最好的语言
Release: 2018-04-03 14:51:40
Original
1978 people have browsed it

This time I will bring you a detailed explanation of using XPath in dom4j (with code). What are the precautions for using XPath in dom4j. Here are practical cases, let’s take a look.

looks like this:

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

<?xml version="1.0" encoding="utf-8"?>
<书架>
   <书>
   <书名>Java就业培训教材</书名>
     <作者>张孝祥</作者>
     <售价>39.00元</售价>
   </书>
   <书>
   <书名>Java网页开发</书名>
     <作者>张孝祥</作者>
     <售价>29.00元</售价>
   </书>
</书架>
Copy after login

users. xml

<?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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How does ajax submit a form and implement file upload

Ajax transmits json format data to the background. How to handle errors

The above is the detailed content of Detailed explanation of using XPath in dom4j (with code). For more information, please follow other related articles on the PHP Chinese website!

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