Castor 进行 XML 数据绑定

今日看了些Castor方面的文章,对castor有了些了解。把用法写在这里,已备我和他人查用.

What Castor?

Java对象与 XML 文档之间来回转换的简单方法

Castor的优势:

1.Castor 几乎是 JAXB 的替代品.可以轻易地将所有 JAXB 代码转变为 Castor

2.提供了 JDO 功能。JDO 也就是 Java Data Objects,是驱动 Java-to-RDBMS 编组和解组的底层技术

3.在数据绑定领域提供了许多的功能,无需使用模式便可在 Java 和 XML 之间进行转换,提供一种比 JAXB 更易于使用的绑定模式,以及能够对关系数据库和 XML 文档进行编组(marshal)和解组(unmarshal)

 

Castor下载:

Castor1.2 http://dist.codehaus.org/castor/1.2/castor-1.2.zip

Castor依赖项http://dist.codehaus.org/castor/1.2/castor-1.2-src.zip 下载解压后在/lib下

 

简单的例子:

拷贝别人的例子,经过测试通过

1.创建Book类

import java.util.LinkedList;
import java.util.List;

public class Book {
 private String isbn; 
 private String title;

private List authors; //作者类对象

 public Book(String isbn, String title, List 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();
  authors.add(author);
 }

 public String getIsbn() {
  return isbn;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getTitle() {
  return title;
 }

 public void setAuthors(List authors) {
  this.authors = authors;
 }

 public List getAuthors() {
  return authors;
 }

public void addAuthor(Author author) {
  authors.add(author);
 }
}

2.创建作者类

public class Author {

 private String firstName, lastName;

 private int totalSales;

public Author(String firstName, String lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setTotalSales(int totalSales) {
  this.totalSales = totalSales;
 }

 public void addToSales(int additionalSales) {
  this.totalSales += additionalSales;
 }

public int getTotalSales() {
  return totalSales;
}
}

3.创建编组器类

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.exolab.castor.xml.Marshaller;

public class BookMarshaller {

 public static void main(String[] args) {
  try {
   Author finder = new Author("Joseph", "Finder");
   Book book = new Book("9780312347482", "Power Play", finder);
   FileWriter writer = new FileWriter("book.xml"); //作者为一人
   Marshaller.marshal(book, writer);

   List book2Authors = new ArrayList();
   book2Authors.add(new Author("Douglas", "Preston"));
   book2Authors.add(new Author("Lincoln", "Child"));
   Book book2 = new Book("9780446618502", "The Book of the Dead",
     book2Authors);//作者为多人
   writer = new FileWriter("book2.xml");
   Marshaller.marshal(book2, writer);

  } catch (Exception e) {
   System.err.println(e.getMessage());
   e.printStackTrace(System.err);
  }
 }
}

 Castor也支持泛型,如果用jdk1.5/1.6开发的话可以在对list<obj>

4.编译并运行后的结果

<?xml version="1.0" encoding="UTF-8"?>
<book><isbn>9780446618502</isbn><title>The Book of the Dead</title>
<author-names xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:type="java:java.lang.String">Douglas Preston</author-names>
<author-names xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:type="java:java.lang.String">Lincoln Child</author-names>
</book>

5.解组图书

public class BookUnmarshaller {

 public static void main(String[] args) {
  try {
   FileReader reader = new FileReader("book.xml");
   Book book = (Book) Unmarshaller.unmarshal(Book.class, 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());
   }

   System.out.println();

reader = new FileReader("book2.xml");
   book = (Book) Unmarshaller.unmarshal(Book.class, reader);
   System.out.println("Book ISBN: " + book.getIsbn());
   System.out.println("Book Title: " + book.getTitle());
   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);
  }
 }
}

运行后结果

Book ISBN: 9780312347482
Book Title: Power Play
Author: Joseph Finder

Book ISBN: 9780446618502
Book Title: The Book of the Dead
Author: Douglas Preston
Author: Lincoln Child

 

需要注意的:

Book类和Author类必须包含空参数的构造方法,因为Castor 主要通过反射和调用 Class.forName(类名).newInstance() 这样的方法进行解组

你可能感兴趣的:(java,xml)