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. 了解discuz!

    discuz!是什么 discuz!是由comsenz出品的,一款使用php编写,以MySQL作为数据存储的社区建站产品,是由戴志康大学时期创始开发,2001年6月发布,如今已被200万网站用做社区建 ...

  2. UVaLive 7359 Sum Kind Of Problem (数学,水题)

    题意:给定一个n,求前 n 个正整数,正奇数,正偶数之和. 析:没什么好说的,用前 n 项和公式即可. 代码如下: #pragma comment(linker, "/STACK:10240 ...

  3. eclipse scons 使用指南

    http://sconsolidator.com/projects/sconsolidator/wiki/Getting_Started Add SCons support to an existin ...

  4. PostgreSQL的initdb 源代码分析之五

    接前面,继续分析: putenv("TZ=GMT") 设置了时区信息. find_other_exec(argv[0], "postgres", PG_BACK ...

  5. 关于MVC中DropDownListFor的一个bug

    如以下代码: //后台 代码 ViewData["source_type"] = new List<SelectListItem> { "}, "} ...

  6. Codeforces Round #330 (Div. 2) A. Vitaly and Night 暴力

    A. Vitaly and Night Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/p ...

  7. 密码强度应用(js)

    <!-- 密码强度div --> <div id="tips" class="help-block"> <b class=&quo ...

  8. asp.net 实现“九连环”小游戏

    asp.net 实现"九连环"小游戏 wildcatsky(原作) public Class ChinaRing1  Inherits System.Web.UI.Page #Re ...

  9. Shell脚本调试工具set

    可以使用set命令的x选项,显示所有命令执行及变量值的变化过程等. 具体使用方法:首先使用set -x开启调试模式,最后使用命令set +x关闭调试模式. 一个简单示例演示如何使用set命令进行脚本调 ...

  10. windows下的python扩展包下载地址

    比如lxml什么的 Unofficial Windows Binaries for Python Extension Packages pip install xxx.whl