Castor XML 之映射模式

Castor 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>


使用映射文件也很简单。
具体的映射规则以后再写。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值