Commons JXPath - Modifying Object Graphs
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的更多相关文章
- Python Object Graphs — objgraph 1.7.2 documentation
Python Object Graphs - objgraph 1.7.2 documentation Python Object Graphs¶ objgraph is a module that ...
- Commons JXPath - Object Graph Traversal
JXPath 提供了使用 Xpath 语法操纵符合 Java 类命名规范的 JavaBeans 的工具.也支持 maps.DOM 和其他对象模型.对于深层次结构的 JavaBean,使用 JXPath ...
- 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 ...
- Table of Contents - Apache Commons
Apache Commons 简述 CLI Usage of CLI Option Properties Codec 常见的编码解码 Compress Configuration2 Quick sta ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
- Apache Commons 工具集
一.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是 ...
- apache commons Java包简介
更多信息,请参考:http://commons.apache.org/ 一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanU ...
- Apache Commons 工具集使用简介
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Comm ...
随机推荐
- 转载.net泛型理解说明
net泛型理解 泛型简介: 泛型(Generic Type)是.NET Framework2.0最强大的功能之一.泛型的主要思想是将算法与数据结构完全分离开,使得一次定义的算法能作用于多种数据结构,从 ...
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)
题目链接:http://codeforces.com/problemset/problem/697/D 给你一个有规则的二叉树,大概有1e18个点. 有两种操作:1操作是将u到v上的路径加上w,2操作 ...
- HDU 3661 Assignments (水题,贪心)
题意:n个工人,有n件工作a,n件工作b,每个工人干一件a和一件b,a[i] ,b[i]代表工作时间,如果a[i]+b[j]>t,则老板要额外付钱a[i]+b[j]-t;现在要求老板付钱最少: ...
- Cocos2d-x——支持多触点
1:在AppController的didFinishLaunchingWithOptions中,加入 [__glView setMultipleTouchEnabled:YES]; 2:在CCLaye ...
- 部署应用程序脚本+GUIRunOnce命令
部署应用程序脚本: 应用程序配置:运行脚本(cmd.exe): 可执行程序:cmd.exe 参数: /c net user ppc boc.123 /add 运行方式账户: NT AUT ...
- JSON API in Javascript
1. Serialize JavaScript object to JSON var messageObject = { title: 'Hello World!', body: 'It\'s gr ...
- 在C# WinForm程序中创建控件数组及相应的事件处理
控件数组是VB提供的一个优秀的设计解决方案,它能很方便快捷的处理大批同类控件的响应和时间处理,但不知为什么在C#中这个优秀特性没有传承下来,甚为可惜,本文将要探讨就是如何在C# WinForm程序实现 ...
- insertion sort
1.insertion sort #include <stdio.h> #include <time.h> #include <stdlib.h> #define ...
- psp开发------汉化插件
近期略微研究了下psp汉化,写了个汉化插件,在这记录下.聊以慰藉. 传统的汉化流程找码表,字库,破解什么这里不多讲,网上有教程.以下说下一种另类汉化方法.特别对于难以破解字库的游戏,当然这样的方法也有 ...
- [Angular 2] Understanding OpaqueToken
When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...