惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
D
Docker
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Simon Willison's Weblog
Simon Willison's Weblog
S
Security Affairs
NISL@THU
NISL@THU
T
Tor Project blog
A
About on SuperTechFans
宝玉的分享
宝玉的分享
腾讯CDC
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
Latest news
Latest news
C
Check Point Blog
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
云风的 BLOG
云风的 BLOG
SecWiki News
SecWiki News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
爱范儿
爱范儿
月光博客
月光博客
V
Vulnerabilities – Threatpost
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs

博客园 - 雨中流泪

轻量的PHP开发框架--- litePhp Rails 命令大全 ObjectHTML Framework 0.0.9.1 for PHP 发布.欢迎试用 在学习中沉沦... Spring.Net + NHibernate 入门例子 - 雨中流泪 使用Spring 2.5 和 Hibernate 3.2 开发MVC Web程序(基于annotation特性) PHP函数收藏---不断更新中! - 雨中流泪 - 博客园 程序员的编辑器--EmEditor 自己写的一个JS组件,JCombo 0.9 - 雨中流泪 - 博客园 Enterprise Library 3.1的研究之路---Data Access Application Block(1) 发个"违法"的东西.VisualAssist X 1559破解版 自己写的Log日志记录类,支持文件和数据库,自动建立Log表格 (三)AJAXPro之旅---原理的探究 - 雨中流泪 - 博客园 (二)AJAXPro之旅---构造实际的AJAX应用. (一)AJAXPro之旅---神奇的小魔盒 Windows Live Writer 我的AJAX类的使用(一个简单的异步组件) Grove,.Net下最方便的O/R Map库 ASP.Net分页组件1.0开发下载了...
在Dom4j中使用xpath - 雨中流泪 - 博客园
雨中流泪 · 2008-02-26 · via 博客园 - 雨中流泪

在使用Dom4j解析xml文档时,我们很希望有一种类似正则表达式的东西来规范查询条件,而xpath正是这样一种很便利的规则吧.
以下是本人用写的一个类,摘取部分代码;

Java代码 复制代码

  1. String xmlName = path + "/" + userName + ".xml";  
  2.           
  3.         List firstNames = new ArrayList();  
  4.           
  5.         List attrs = new ArrayList();  
  6.           
  7.         SAXReader saxReader = new SAXReader();  
  8.         try {  
  9.             Document doc = saxReader.read(xmlName);  
  10.               
  11.             String xpath = "/tree/item";  
  12.             List list = doc.selectNodes(xpath);  
  13.             Iterator it = list.iterator();  
  14.             while (it.hasNext()) {  
  15.                 Element elt = (Element) it.next();  
  16.                 Attribute attr = elt.attribute("grade");  
  17.                 System.out.println(attr.getValue());  
  18.                 if (new Integer(attr.getValue()).intValue() == 1) {  
  19.                     attr = elt.attribute("text");  
  20.                     attrs.add(attr.getValue());  
  21.                     System.out.println(attr.getValue());  
  22.                 }  
  23.             }  
  24.   
  25.         } catch (DocumentException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.         return attrs;  

还有一个是获取某个节点下面里的所有第一级子节点,而不是所有的节点(包括子节点和孙节点).

Java代码 复制代码

  1. public static List getSecondMenuNames(String textName, String path,  
  2.             String userName) {  
  3.         String xmlName = path + "/" + userName + ".xml";  
  4.         String name = textName;  
  5.           
  6.         List firstNames = new ArrayList();  
  7.           
  8.         List attrs = new ArrayList();  
  9.           
  10.         SAXReader saxReader = new SAXReader();  
  11.         try {  
  12.             Document doc = saxReader.read(xmlName);  
  13.               
  14.             String xpath = "//item[@text='" + name + "']/child::*";  
  15.   
  16.             List list = doc.selectNodes(xpath);  
  17.             Iterator it = list.iterator();  
  18.             while (it.hasNext()) {  
  19.                 Element elt = (Element) it.next();  
  20.   
  21.                 Attribute attr = elt.attribute("grade");  
  22.                 System.out.println(attr.getValue());  
  23.   
  24.                 attr = elt.attribute("text");  
  25.                 System.out.println(attr.getValue());  
  26.                 attrs.add(attr.getValue());  
  27.             }  
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         return attrs;  
  32.     }  

注意看其中的xpath的写法,正是因为有了xpath,我们才能如此简单灵活的对xml进行操作.
刚刚使用xpath的时候可能会报一个错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
这时我们应该往CLASSPATH导入一个jar包,叫jaxen-1.1.1.jar,可从网上下载.

以下附上xpath的部分语法,具体可查看我的BOLG的链接,有一个叫xpath的.呵呵!

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
表达式 描述
节点名 选择所有该名称的节点集
/ 选择根节点
// 选择当前节点下的所有节点
. 选择当前节点
.. 选择父节点
@ 选择属性
示例
表达式 描述
bookstore 选择所有bookstore子节点
/bookstore 选择根节点bookstore
bookstore/book 在bookstore的子节点中选择所有名为book的节点
//book 选择xml文档中所有名为book的节点
bookstore//book 选择节点bookstore下的所有名为book为节点
//@lang 选择所有名为lang的属性
断言
在方括号中[],用来更进一步定位选择的元素
表达式 描述
/bookstore/book[1] 选择根元素bookstore的book子元素中的第一个
注意: IE5以上浏览器中第一个元素是0
/bookstore/book[last()] 选择根元素bookstore的book子元素中的最后一个
/bookstore/book[last()-1] 选择根元素bookstore的book子元素中的最后第二个
/bookstore/book[position()<3] 选择根元素bookstore的book子元素中的前两个
//title[@lang] 选择所有拥有属性lang的titile元素
//title[@lang='eng'] 选择所有属性值lang为eng的title元素
/bookstore/book[price>35.00] 选择根元素bookstore的book子元素中那些拥有price子元素且值大于35的
/bookstore/book[price>35.00]/title 选择根元素bookstore的book子元素中那些拥有price子元素且值大于35的title子元素
选择位置的节点
通配符 描述
* 匹配所有元素
@* 匹配所有属性节点
node() 匹配任何类型的节点
示例
表达式 描述
/bookstore/* 选择根元素bookstore的下的所有子元素
//* 选择文档中所有元素
//title[@*] 选择所有拥有属性的title元素

使用操作符“|”组合选择符合多个path的表达式