我是靠谱客的博主 满意冰棍,这篇文章主要介绍在dom4j中使用XPath的简单实例,现在分享给大家,希望可以做个参考。

如下所示:

复制代码
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("登录成功"); } } }

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元</售价> </书> </书架>

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>

以上就是小编为大家带来的在dom4j中使用XPath的简单实例的全部内容了,希望对大家有所帮助,多多支持脚本之家~

最后

以上就是满意冰棍最近收集整理的关于在dom4j中使用XPath的简单实例的全部内容,更多相关在dom4j中使用XPath内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(175)

评论列表共有 0 条评论

立即
投稿
返回
顶部