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");

 

posted on 2015-08-02 14:49  huey2672  阅读(711)  评论(0编辑  收藏  举报