Castor XML 的映射模式可以满足内省模式所不能处理的需求。
在映射模式下,Java 类及属性与XML 文档的元素、属性之间的绑定关系是写在XML形式的文件里的。
下面是Java 类:
要解组的数据文件:
Author类的映射文件:
Book类的映射文件
根据映射文件解组的代码:
输出的结果:
2011-6-2 10:43:54 org.exolab.castor.mapping.Mapping setBaseURL
信息: book_map.xml is not a URL, trying to convert it to a file URL
2011-6-2 10:43:54 org.exolab.castor.mapping.Mapping loadMapping
信息: Loading mapping descriptors from book_map.xml
2011-6-2 10:43:54 org.exolab.castor.mapping.Mapping loadMapping
信息: Loading mapping descriptors from author_map.xml
Book ISBN: 9780446618502
Book Title: The Book of the Dead
Author: Douglas Preston
Author: Lincoln Child
根据映射文件编组的代码:
编组生成的XML文件:
使用映射文件也很简单。
具体的映射规则以后再写。
在映射模式下,Java 类及属性与XML 文档的元素、属性之间的绑定关系是写在XML形式的文件里的。
下面是Java 类:
public class Author {
private String firstName, lastName;
public Author() {
}
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
public class Book {
private String isbn;
private String title;
private List<Author> authors;
public Book() {
}
public Book(String isbn, String title, List<Author> authors) {
this.isbn = isbn;
this.title = title;
this.authors = authors;
}
public Book(String isbn, String title, Author author) {
this.isbn = isbn;
this.title = title;
this.authors = new LinkedList<Author>();
authors.add(author);
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getIsbn() {
return isbn;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setAuthors(List<Author> authors) {
this.authors = authors;
}
public List<Author> getAuthors() {
return authors;
}
public void addAuthor(Author author) {
authors.add(author);
}
}
要解组的数据文件:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<authors>
<author>
<name first="Douglas" last="Preston" />
</author>
<author>
<name first="Lincoln" last="Child" />
</author>
</authors>
<book-info>
<isbn>9780446618502</isbn>
<title>The Book of the Dead</title>
</book-info>
</book>
Author类的映射文件:
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<class name="liuwh.learn.databind.castor.test3.Author">
<field name="FirstName" type="java.lang.String">
<bind-xml name="first" node="attribute" location="name" />
</field>
<field name="LastName" type="java.lang.String">
<bind-xml name="last" node="attribute" location="name" />
</field>
</class>
</mapping>
Book类的映射文件
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<!-- name 属性中指定完全限定的 Java 类名 -->
<class name="liuwh.learn.databind.castor.test3.Book">
<!-- xml 属性指定这个类要映射到的 XML 元素 -->
<map-to xml="book" />
<!-- name 指定POJO的属性名,不是成员变量名。Castor调用set[PropertyName]()设置值 -->
<!-- type指定数据类型,必须是全限定类名 -->
<field name="Title" type="java.lang.String">
<!-- node 指定是绑定到属性(attribute)还是绑定到元素(element) -->
<bind-xml name="title" node="element" location="book-info" />
</field>
<field name="Isbn" type="java.lang.String">
<bind-xml name="isbn" node="element" location="book-info" />
</field>
<field name="Authors" type="liuwh.learn.databind.castor.test3.Author"
collection="arraylist">
<!--
一个类中的一个字段并不直接映射到与这个类对应的 XML 元素中的数据 — 就需要在 bind-xml 元素中使用 location 属性
如果绑定到元素数据,它就应该是目标元素的父 元素;如果绑定到属性数据,它就应该是包含这个属性的 元素。
-->
<bind-xml name="author" location="authors" />
</field>
</class>
</mapping>
根据映射文件解组的代码:
public class BookMapUnmarshaller {
public static void main(String[] args) {
Mapping mapping = new Mapping();
try {
mapping.loadMapping("book_map.xml");
mapping.loadMapping("author_map.xml");
XMLContext context = new XMLContext();
context.addMapping(mapping);
Unmarshaller unmarshaller = context.createUnmarshaller();
FileReader reader = new FileReader("bookdata.xml");
Book book = (Book) unmarshaller.unmarshal(reader);
System.out.println("Book ISBN: " + book.getIsbn());
System.out.println("Book Title: " + book.getTitle());
List authors = book.getAuthors();
for (Iterator i = authors.iterator(); i.hasNext();) {
Author author = (Author) i.next();
System.out.println("Author: " + author.getFirstName() + " "
+ author.getLastName());
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
}
输出的结果:
2011-6-2 10:43:54 org.exolab.castor.mapping.Mapping setBaseURL
信息: book_map.xml is not a URL, trying to convert it to a file URL
2011-6-2 10:43:54 org.exolab.castor.mapping.Mapping loadMapping
信息: Loading mapping descriptors from book_map.xml
2011-6-2 10:43:54 org.exolab.castor.mapping.Mapping loadMapping
信息: Loading mapping descriptors from author_map.xml
Book ISBN: 9780446618502
Book Title: The Book of the Dead
Author: Douglas Preston
Author: Lincoln Child
根据映射文件编组的代码:
public class BookMapMarshaller {
public static void main(String[] args) {
List<Author> authors = new ArrayList<Author>();
Author author = new Author("liu", "yi");
authors.add(author);
Author author2 = new Author("liu", "er");
authors.add(author2);
Book book = new Book("isbn123456", "<<深入计算机操作系统>>", authors);
Mapping mapping = new Mapping();
try {
mapping.loadMapping("book_map.xml");
mapping.loadMapping("author_map.xml");
XMLContext context = new XMLContext();
context.addMapping(mapping);
Marshaller marshaller = context.createMarshaller();
FileWriter writer = new FileWriter("map_marshaller.xml");
marshaller.setWriter(writer);
marshaller.marshal(book);
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
}
编组生成的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<book-info>
<title><<深入计算机操作系统>></title>
<isbn>isbn123456</isbn>
</book-info>
<authors>
<author>
<name first="liu" last="yi" />
</author>
<author>
<name first="liu" last="er" />
</author>
</authors>
</book>
使用映射文件也很简单。
具体的映射规则以后再写。