JXPath 提供了使用 Xpath 语法操纵符合 Java 类命名规范的 JavaBeans 的工具。也支持 maps、DOM 和其他对象模型。对于深层次结构的 JavaBean,使用 JXPath 可以方便地访问深层次的属性,而免去了繁琐的 getter/setter 操作。

以下面的 JavaBeans 为例。

package com.huey.jxpath;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
public class Book { private String title;
private Author[] authors;
private Publisher publisher;
private String isbn;
private double price; }

Book.java

package com.huey.jxpath;

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
public class Author { private String firstName;
private String lastName;
private char gender;
private Date birthday; }

Author.java

package com.huey.jxpath;

import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
public class Publisher { private String name;
private String address;
private Map<String, String> contacts; }

Publisher.java

初始化

Author[] authors;
Publisher publisher;
Book book; authors = new Author[] {
new Author("Eric", "Freeman", 'F', new Date()),
new Author("ElElisabeth", "Freeman", 'M', new Date())
}; Map<String, String> contacts = new HashMap<String, String>();
contacts.put("tel", "010-12345678");
contacts.put("fax", "010-87654321");
contacts.put("email", "test@163.com");
publisher = new Publisher("中国电力出版社", "北京市XX区YY路Z号", contacts); book = new Book("Head First Design Patterns", authors, publisher, "9787508353937", 98.0);

JavaBean Property Access

JXPathContext context = JXPathContext.newContext(book);
String title = (String) context.getValue("title");
Publisher publisher = (Publisher) context.getValue("publisher");

Lenient Mode

当提供的 xpath 无法映射到 JavaBean 的属性时,context.getValue(xpath) 方法会抛出一个异常;而如果调用方法 context.setLenient(true),则会返回 null。

Nested Bean Property Access

JXPathContext context = JXPathContext.newContext(book);
String pName = (String)context.getValue("publisher/name");
char aGender = (Character)context.getValue("authors[firstName='Eric']/gender");

Collection Subscripts

JXPathContext context = JXPathContext.newContext(book);
Author author = (Author) context.getValue("authors[1]"); // 集合第一个元素的下标是 1 而不是 0

Retrieving Multiple Results

JXPathContext context = JXPathContext.newContext(book);
Iterator<?> authors = context.iterate("authors");
while (authors.hasNext()) {
Author author = (Author) authors.next();
// ...
}

Map Element Access

JXPathContext context = JXPathContext.newContext(publisher);
String tel = (String) context.getValue("contacts/tel");
String fax = (String) context.getValue("contacts[@name='fax']");
String email = (String) context.getValue("contacts/attribute::email");

Commons JXPath - Object Graph Traversal的更多相关文章

  1. Commons JXPath - Modifying Object Graphs

    JXPath 除了可以 XPath 语法访问 JavaBeans.DOM/JDOM,也可以对其属性赋值. 以下面的 JavaBeans 为例. package com.huey.jxpath; imp ...

  2. [Javascript] Creating an Immutable Object Graph with Immutable.js Map()

    Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...

  3. Xwork概况 XWork是一个标准的Command模式实现,并且完全从web层脱离出来。Xwork提供了很多核心功能:前端拦截机(interceptor),运行时表单属性验证,类型转换,强大的表达式语言(OGNL – the Object Graph NavigationLanguage),IoC(Inversion of Control反转控制)容器等。 ----------------

    Xwork概况 XWork是一个标准的Command模式实现,并且完全从web层脱离出来.Xwork提供了很多核心功能:前端拦截机(interceptor),运行时表单属性验证,类型转换,强大的表达式 ...

  4. [Algorithms] Graph Traversal (BFS and DFS)

    Graph is an important data structure and has many important applications. Moreover, grach traversal ...

  5. Commons JXPath - DOM/JDOM Document Access

    除了 JavaBean,JXPath 也可以访问 DOM/JDOM. 示例 XML: <?xml version="1.0" encoding="utf-8&quo ...

  6. Commons JXPath - Extension Functions

    Standard Extension Functions 创建新的对象 JXPathContext context = JXPathContext.newContext(null); Book boo ...

  7. Object Graph Serialization

    http://coding-time.blogspot.com/2008/03/serialize-object-graph-to-xml-in-net.html http://trycatch.me ...

  8. Struts_OGNL(Object Graph Navigation Language) 对象图导航语言

    1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...

  9. Table of Contents - Apache Commons

    Apache Commons 简述 CLI Usage of CLI Option Properties Codec 常见的编码解码 Compress Configuration2 Quick sta ...

随机推荐

  1. Mysql知识要点总结

    1.安装 要点:记得更改字符集 2.数据类型 常用数据类型:INT VARCHAR BLOG 3.操作数据库 SHOW DATABASES; CREATE DATABASE 名称; DROP DATA ...

  2. angular 管理后台

    http://blog.csdn.net/iamnieo/article/details/50474399

  3. 【三支火把】---CDS5516舵机调试以及自己的感想!

    我依然坚持,任何一次自己的心有感触都要及时的记录下来,这样你的努力才是真正的努力. 这两天一直在用STM32调试CDS5516舵机,其实很简单,但是却花了将近两天的时间,过程之曲折我就不说了,先总结一 ...

  4. hadoop2.1.0编译安装教程

    由于现在hadoop2.0还处于beta版本,在apache官方网站上发布的beta版本中只有编译好的32bit可用,如果你直接下载安装在64bit的linux系统的机器上,运行会报一个INFO ut ...

  5. nginx 的安装

    一.必要软件准备1.安装pcre 为了支持rewrite功能,我们需要安装pcre 复制代码代码如下: # yum install pcre* //如过你已经装了,请跳过这一步 2.安装openssl ...

  6. linux中crontab实现以秒执行任务

    用crontab+sleep实现以秒执行任务 crontab -e * * * * * /bin/date >>/tmp/date.txt * * * * * sleep 10s; /bi ...

  7. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  8. Js Pattern - Self Define Function

    This pattern is useful when your function has some initial preparatory work to do andit needs to do ...

  9. codeforces Gym 100500C D.Hall of Fame 排序

    Hall of Fame Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachmen ...

  10. .NET中的三种Timer的区别和用法

    最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: //1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应 ...