Commons JXPath - Object Graph Traversal
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的更多相关文章
- Commons JXPath - Modifying Object Graphs
JXPath 除了可以 XPath 语法访问 JavaBeans.DOM/JDOM,也可以对其属性赋值. 以下面的 JavaBeans 为例. package com.huey.jxpath; imp ...
- [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 ...
- Xwork概况 XWork是一个标准的Command模式实现,并且完全从web层脱离出来。Xwork提供了很多核心功能:前端拦截机(interceptor),运行时表单属性验证,类型转换,强大的表达式语言(OGNL – the Object Graph NavigationLanguage),IoC(Inversion of Control反转控制)容器等。 ----------------
Xwork概况 XWork是一个标准的Command模式实现,并且完全从web层脱离出来.Xwork提供了很多核心功能:前端拦截机(interceptor),运行时表单属性验证,类型转换,强大的表达式 ...
- [Algorithms] Graph Traversal (BFS and DFS)
Graph is an important data structure and has many important applications. Moreover, grach traversal ...
- Commons JXPath - DOM/JDOM Document Access
除了 JavaBean,JXPath 也可以访问 DOM/JDOM. 示例 XML: <?xml version="1.0" encoding="utf-8&quo ...
- Commons JXPath - Extension Functions
Standard Extension Functions 创建新的对象 JXPathContext context = JXPathContext.newContext(null); Book boo ...
- Object Graph Serialization
http://coding-time.blogspot.com/2008/03/serialize-object-graph-to-xml-in-net.html http://trycatch.me ...
- Struts_OGNL(Object Graph Navigation Language) 对象图导航语言
1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...
- Table of Contents - Apache Commons
Apache Commons 简述 CLI Usage of CLI Option Properties Codec 常见的编码解码 Compress Configuration2 Quick sta ...
随机推荐
- 沉金板VS 镀金板
沉金板VS 镀金板一.沉金板与镀金板的区别1.原理区别FLASH GOLD 采用的是化学沉积的方法!PLANTINGGOLD 采用的是电解的原理!2.外观区别电金会有电金引线,而化金没有.而且若金厚要 ...
- myGeneration代码生成器
转自:http://www.cnblogs.com/leitwolf/archive/2007/07/27/833255.html http://blog.csdn.net/happyhippy/ar ...
- mysql CASE WHEN的基础和多种用法
CASE计算条件列表并返回多个可能结果表达式之一. CASE 具有两种格式: 简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果. CASE 搜索函数计算一组布尔表达式以确定结果. 两 ...
- android ipc通信机制之二序列化接口和Binder
IPC的一些基本概念,Serializable接口,Parcelable接口,以及Binder.此核心为最后的IBookManager.java类!!! Serializable接口,Parcelab ...
- mac 杂谈
========== 下载 -o 文件名:-O默认文件名保存 curl -o baidu.hml http://www.baidu.com curl -O http://su.bdimg.com/st ...
- java中匿名类的讲解
匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实例1:不使用匿名内部类来实现抽象 ...
- 常见AutoCAD病毒(acad.fas、acad.lsp)清除方法
常见AutoCAD病毒(acad.fas.acad.lsp)清除方法 acad.fas.acad.lsp这两种病毒是最常见的CAD病毒了,而且往往同一时候出现.因为其本身对系统并不具备危害性,不过恶作 ...
- ZJU-PAT 1065. A+B and C (64bit) (20)
Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Input S ...
- [Angular 2] Factory Provider with dependencies
This lesson discusses when and how to add dependencies, resolved by Angular’s DI, to factory provide ...
- Asp.Net+Extjs实现登录
通过对Ext的学习,发现学习分三部曲:1.看官网的Demo,宏观了解Ext能做什么:2.看相关书籍,做理论指导:3.实现官网的Demo,体会Ext的真谛. 在完毕了第一.二部后,如今我们须要做的是实现 ...