码迷,mamicode.com
首页 > 其他好文 > 详细

JXPath(1.3) - Object Graph Traversal

时间:2015-08-02 16:23:51      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

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

 

JXPath(1.3) - Object Graph Traversal

标签:

原文地址:http://www.cnblogs.com/huey/p/4695448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!