1、XStream,XStream是codehaus上的一个开源项目,用于将Javabean serialize为xml格式,以及从xml中生成javabean。
2、castor、xmlbeans,castor和xmlbeans这种都是先定义schema,然后根据schema来generate java class,然后调用marshall和unmarshall方法来序列化javabean。这是属于重量级的方法,虽然功能强大,使用却不方便。如果你改了schema,势必要重新generate,这样你就不能在generate的java class上加上自己的方法。导致生成的java class纯粹用于serialize。
3、 commons的betwikt也具有类似的功能,但好像需要配置rule,显得还是稍微麻烦些。
xMap具体实现:
package test;
import java.util.ArrayList;
import java.util.List;
import org.nuxeo.common.xmap.annotation.XNode;
import org.nuxeo.common.xmap.annotation.XNodeList;
import org.nuxeo.common.xmap.annotation.XObject;
/**
* Book 实体对象,此处用XMap注解
* @author Administrator
*
*/
@XObject
public class Book {
@XNode("title")
private String title;
@XNode("@no")
private String no;
@XNodeList(value = "subject", type = ArrayList.class, componentType = String.class)
private List<String> subjects;
@XNode("price")
private int price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getSubjects() {
return subjects;
}
public void setSubjects(List<String> subjects) {
this.subjects = subjects;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
/**==================================================**/
package test;
import java.util.ArrayList;
import java.util.List;
import org.nuxeo.common.xmap.annotation.XNodeList;
import org.nuxeo.common.xmap.annotation.XObject;
/**
* 图书对象集合类
* @author zhouyu
*
*/
@XObject(value = "books")
public class Books {
@XNodeList(value = "book", type = ArrayList.class, componentType = Book.class)
private List<Book> book;
public List<Book> getBook() {
return book;
}
public void setBook(List<Book> book) {
this.book = book;
}
public void addBook(Book b){
if(book==null){
book=new ArrayList<Book>();
}
book.add(b);
}
}
/**==================================================**/
package test;
import java.util.ArrayList;
import java.util.List;
import org.nuxeo.common.xmap.XMap;
import com.io.IoUtils;
/**
* 从对象到xml转换
* @author zhouyu
*
*/
public class ObjectToXmlDemo {
public static void main(String[] args) {
try {
//声明一组书的根节点
Books books=new Books();
//声明一本书
Book book=new Book();
//书的类型
List<String> sj = new ArrayList<String>();
sj.add("java");
sj.add("xml");
//设置书节点的属性
book.setPrice(10);
book.setTitle("java是傻逼");
book.setSubjects(sj);
book.setNo("1");
//将书节点添加到跟节点
books.addBook(book);
//声明一本书
Book book2=new Book();
//书的类型
List<String> sj2=new ArrayList<String>();
sj2.add("C++");
sj2.add("c");
//设置书节点的属性
book2.setPrice(10);
book2.setTitle("c++程序设计");
book2.setSubjects(sj2);
book2.setNo("2");
//将书节点添加到跟节点
books.addBook(book2);
XMap xmp = new XMap();
xmp.register(Books.class);
List<String> filters = new ArrayList<String>();
String content = xmp.asXmlString(books, "UTF-8", filters, true);
IoUtils.WriteFile(content, "UTF-8", "books.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**==================================================**/
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import org.nuxeo.common.xmap.XMap;
public class XmlToObjectDemo {
public static void main(String[] args) {
try {
XMap xmp = new XMap();
xmp.register(Books.class);
//URL url = new File("books.xml").toURL();
InputStream in = new FileInputStream(new File("books.xml"));
Object[] result = (Object[]) xmp.loadAll(in);
Books books = (Books) result[0];
for(int i = 0 ; i < books.getBook().size() ; i++)
{
Book book = books.getBook().get(i);
System.out.println(book.getNo());
System.out.println(book.getPrice());
System.out.println(book.getTitle());
System.out.println(book.getSubjects().get(i));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**==================================================**/
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books>
<book no="1">
<title>java是傻逼</title>
<subject>java</subject>
<subject>xml</subject>
<price>10</price>
</book>
<book no="2">
<title>c++程序设计</title>
<subject>C++</subject>
<subject>c</subject>
<price>10</price>
</book>
</books>