JXPath(1.3) - Modifying Object Graphs
2015-08-03 14:07
477 查看
JXPath 除了可以 XPath 语法访问 JavaBeans、DOM/JDOM,也可以对其属性赋值。
以下面的 JavaBeans 为例。
Publisher.java
初始化:
Setting Properties
Creating Objects
当对 JavaBean 的复杂数据类型属性设置值时,如果属性没有实例化,则会抛出一个 JXPathException 异常。实现 AbstractFactory 接口后,再调用 context.createPath 方法,能够在复杂数据类型对象为 null 时,为其实例化。context.createPathAndSetValue 方法能够在实例化对象的同时设置值。
以下面的 JavaBeans 为例。
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");
相关文章推荐
- oc 用来取消某个延时的动作接口NSObject cancelPreviousPerformRequestsWithTarget
- ObjectARX非模态对话框为当前焦点
- object detection--decision forests(DF)--VJ
- php的get_object_vars函数
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- iOS开发系列--Objective-C之KVC、KVO
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- objective-c内存解析
- iOS事件处理——Swift & Objective-C 表述
- Objective-C发展历史
- Objective-C发展历史
- ObjectARX下载ObjectARX Wizards
- 【Objective-C编程】Objective-C的基本数据类型
- [objective-c] 面向对象1
- objective C程序 封装、继承及多态
- [Objective-c] OC介绍
- Objective-C学习笔记_属性
- 多线程程序设计学习(13)Active Object pattern
- Objective-C—— Block
- JSON4:嵌套的JsonObject与JSONArray的取值