JXPath 除了可以 XPath 语法访问 JavaBeans、DOM/JDOM,也可以对其属性赋值。

以下面的 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);

Setting Properties

JXPathContext context = JXPathContext.newContext(book);
context.setValue("publisher/name", "人民邮电出版社");
context.setValue("publisher/contacts/attribute::email", "test@gmail.com");

Creating Objects

当对 JavaBean 的复杂数据类型属性设置值时,如果属性没有实例化,则会抛出一个 JXPathException 异常。实现 AbstractFactory 接口后,再调用 context.createPath 方法,能够在复杂数据类型对象为 null 时,为其实例化。context.createPathAndSetValue 方法能够在实例化对象的同时设置值。

package com.huey.jxpath;

import java.lang.reflect.Array;
import java.util.HashMap; import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer; public class BookFactory extends AbstractFactory { @Override
public boolean createObject(JXPathContext context, Pointer pointer,
Object parent, String name, int index) { if (parent instanceof Book && "authors".equals(name)) {
Book book = (Book) parent;
if (book.getAuthors() == null) {
book.setAuthors(new Author[]{});
}
int newSize = index + 1;
int oldSize = book.getAuthors().length;
if (newSize > oldSize) {
Author[] newAuthors = (Author[]) resizeArray(book.getAuthors(), newSize);
book.setAuthors(newAuthors);
}
return true;
}
if (parent instanceof Book && "publisher".equals(name)) {
((Book)parent).setPublisher(new Publisher());
return true;
}
if (parent instanceof Publisher && "contacts".equals(name)) {
((Publisher)parent).setContacts(new HashMap<String, String>());
return true;
}
return false;
} /**
* 调整数组长度,当新的数组长度大于旧的数组长度时,实例化所有新增的元素
* @param oldArray
* @param newSize
* @return
*/
private Object resizeArray (Object oldArray, int newSize) {
int oldSize = Array.getLength(oldArray);
Class<?> elementType = oldArray.getClass().getComponentType(); Object newArray = Array.newInstance(elementType, newSize);
int preserveLength = Math.min(oldSize, newSize);
if (preserveLength > 0) {
System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
}
try {
for (int i = preserveLength; i < newSize; i++) {
Array.set(newArray, i, elementType.newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
return newArray;
} }
JXPathContext context = JXPathContext.newContext(new Book());
context.setFactory(new BookFactory());
context.createPathAndSetValue("title", "hello jxpath");
context.createPathAndSetValue("authors[1]/gender", 'F');
// Map 对象的 xpath 必须使用 contacts/email 或 contacts/child::email 而不能使用 contacts[@email] 或 contacts/attribute::email
context.createPathAndSetValue("publisher/contacts/child::email", "test@gmial.com");

Commons JXPath - Modifying Object Graphs的更多相关文章

  1. Python Object Graphs — objgraph 1.7.2 documentation

    Python Object Graphs - objgraph 1.7.2 documentation Python Object Graphs¶ objgraph is a module that ...

  2. Commons JXPath - Object Graph Traversal

    JXPath 提供了使用 Xpath 语法操纵符合 Java 类命名规范的 JavaBeans 的工具.也支持 maps.DOM 和其他对象模型.对于深层次结构的 JavaBean,使用 JXPath ...

  3. Commons JXPath - DOM/JDOM Document Access

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

  4. Commons JXPath - Extension Functions

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

  5. Table of Contents - Apache Commons

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

  6. 编写更少量的代码:使用apache commons工具类库

    Commons-configuration   Commons-FileUpload   Commons DbUtils   Commons BeanUtils  Commons CLI  Commo ...

  7. Apache Commons 工具集

    一.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是 ...

  8. apache commons Java包简介

    更多信息,请参考:http://commons.apache.org/ 一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanU ...

  9. Apache Commons 工具集使用简介

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Comm ...

随机推荐

  1. 33条C#、.Net经典面试题目及答案[zt]

    33条C#..Net经典面试题目及答案[zt] 本文集中了多条常见的C#..Net经典面试题目例如“.NET中类和结构的区别”.“ASP.NET页面之间传递值的几种方式?”,并简明扼要的给出了答案,希 ...

  2. 简单的玩玩etimer <contiki学习笔记之九 补充>

    这幅图片是对前面  <<contiki学习笔记之九>>  的一个补充说明. 简单的玩玩etimer <contiki学习笔记之九> 或许,自己正在掀开contiki ...

  3. C#的图片拼接

    貌似很长时间没有写博客了,感觉再不写都要废了. 这段时间确实迷茫得不行,整天混混顿顿的,逃避这个逃避那个,话说已经辞职一个月了…… 这几天在学用libgdx做安卓上的游戏,感觉缺少一个图片拼接的工具, ...

  4. Swift学习笔记五

    基础运算符 Swift的大部分运算符和C及OC相同,也分一元二元多元的,这里只记录一些Swift特有的性质或写法. 赋值运算符( = ) 在等号右边是一个有多个值的元组时,它的成员值可以分解并同时分别 ...

  5. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  6. Missing access checks in put_user/get_user kernel API (CVE-2013-6282)

    /* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰    邮箱: minzhenfei@163.com */ 1.漏洞成因 Linux kernel对ARM上的get_user/put_us ...

  7. Perl多进程

    perl作为一种解释性的语言,非常受广大系统管理员的欢迎,优点么就不多说了,坏处也有不少,比如对线程的支持,就一直不咋地,所以大多数情况下,我们都须要多个进程,来帮助我们完毕工作,闲话少说,上代码. ...

  8. myloader原理0

    开源MySQL多线程逻辑导入工具myloader原理与改进 在上一篇中,介绍了多线程备份工具mydumper的实现及网易对其所做的优化,本篇聊聊与mydumper配合使用的myloader工具. my ...

  9. C#_ajax fileupload

    @{ ViewBag.Title = "Index"; Layout = null; } <h2>Index</h2> <script src=&qu ...

  10. 升级时出现:请先升级 UCenter 到 1.6.0 以上版本。

    有的站点UCenter升级完成后仍然提示请先升级 UCenter 到 1.6.0 以上版本的现象,下面分享下UCenter版本号不正确的原因和处理办法,可能有以下的几个文件和处理办法: 一.UCent ...