JXPath提供了使用Xpath语法操纵符合Java类命名规范的 JavaBeans的工具。也支持maps,DOM和其他对象模型。.
1. 作为示例的JavaBean:
package com.huey.dream.jxpath;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Writer {
@Getter @Setter
private String name;
@Getter @Setter
private char gender;
@Getter @Setter
private Date birthday;
}
package com.huey.dream.jxpath;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Publisher {
@Getter @Setter
private String name;
@Getter @Setter
private String address;
@Getter @Setter
private Map<String, String> contacts;
}
package com.huey.dream.jxpath;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Book {
@Getter @Setter
private String title;
@Getter @Setter
private Writer[] authors;
@Getter @Setter
private Publisher publisher;
@Getter @Setter
private String isbn;
@Getter @Setter
private double price;
}
2. 初始化对象实例:
Writer[] writers;
Publisher publisher;
Book book;
writers = new Writer[] {
new Writer("Eric Freeman", 'F', new Date()),
new Writer("ElElisabeth Freeman", 'M', new Date()),
};
Map<String, String> contacts = new HashMap<String, String>();
contacts.put("tel", "010-12345678");
contacts.put("fax", "010-87654321");
publisher = new Publisher("中国电力出版社", "北京市XX区YY路Z号", contacts);
book = new Book("Head First Design Patterns", writers, publisher, "9787508353937", 98.0);
3. JavaBean Property Access:
JXPathContext context = JXPathContext.newContext(writers[0]);
String wName = (String)context.getValue("name");
4. Lenient Mode:
当提供的xpath无法映射到JavaBean的属性时,context.getValue(xpath)方法会抛出一个异常;而如果调用方法context.setLenient(true),则会返回null。
5. Nested Bean Property Access:
JXPathContext context = JXPathContext.newContext(book);
Publisher pub = (Publisher)context.getValue("publisher");
String pName = (String)context.getValue("publisher/name");
char aGender = (Character)context.getValue("authors[name='Eric Freeman']/gender");
6. Collection Subscripts:
JXPathContext context = JXPathContext.newContext(book);
// 在XPath中,集合的首个元素的下标是1而不是0
Writer author = (Writer)context.getValue("authors[1]");
7. Retrieving Multiple Results:
JXPathContext context = JXPathContext.newContext(book);
Iterator<?> authors = context.iterate("authors");
while (authors.hasNext()) {
Writer author = (Writer)authors.next();
System.out.println(author);
}
8. Map Element Access:
JXPathContext context = JXPathContext.newContext(publisher);
String tel = (String)context.getValue("contacts/tel");
String fax = (String)context.getValue("contacts[@name='fax']");
9. Setting Properties:
JXPathContext context = JXPathContext.newContext(publisher);
String tel = (String)context.getValue("contacts/tel");
String fax = (String)context.getValue("contacts[@name='fax']");
10. Creating Objects:
package com.huey.dream.jxpath;
import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
public class PublisherFactory extends AbstractFactory {
@Override
public boolean createObject(JXPathContext context, Pointer pointer,
Object parent, String name, int index) {
if (parent instanceof Book && "publisher".equals(name)) {
((Book)parent).setPublisher(new Publisher());
return true;
}
return false;
}
}
Book book = new Book();
JXPathContext context = JXPathContext.newContext(book);
context.setFactory(new PublisherFactory());
context.createPath("publisher");
// 即创建路径又对其赋值
context.createPathAndSetValue("publisher", publisher);
System.out.println(book);
11. Variables:
JXPathContext context = JXPathContext.newContext(book);
context.getVariables().declareVariable("index", new Integer(2));
Writer secondAuthor = (Writer)context.getValue("authors[$index]");
12. Pointer:
JXPathContext context = JXPathContext.newContext(book);
Pointer pointer = context.getPointer("authors[gender='M']/name");
System.out.println(pointer); // "/authors[2]/name"
13. Relative Contexts:
JXPathContext context = JXPathContext.newContext(book);
Pointer pointer = context.getPointer("authors[2]");
JXPathContext relativeContext = context.getRelativeContext(pointer);
// 相对路径
String wName = (String)relativeContext.getValue("name");
System.out.println(wName);
// 绝对路径
char wGender = (Character)relativeContext.getValue("/authors[2]/gender");
System.out.println(wGender);
//
String pName = (String)relativeContext.getValue("../publisher/name");
System.out.println(pName);
14. Standard Extension Functions:
JXPathContext context = JXPathContext.newContext(null);
Writer writer = (Writer)context.getValue("com.huey.dream.jxpath.Writer.new()");
15. Custom Extension Functions:
package com.huey.dream.jxpath;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formats {
public static String date2Str(Date date, String pattern){
return new SimpleDateFormat(pattern).format(date);
}
}
JXPathContext context = JXPathContext.newContext(writers[0]);
context.setFunctions(new ClassFunctions(Formats.class, "formats"));
String birthday = (String)context.getValue("formats:date2Str(birthday, 'yyyy-MM-dd')");
System.out.println(birthday);