XML和Java Bean互相转换是一个很有用的功能,因为两者有着前后合作的关系,但解析的过程比较痛苦。下面介绍如何进行XML和Java
Bean的互相转换。
 最近项目中用到了xml和java bean的转换
 用到xml的时候我们需要遍历解析。。。然后将解析的值放入声明好的java bean,过程很痛苦,结果很美好
 我们能不能直接从xml转化到java bean呢,或者直接成java bean转化到xml呢?
 答案是OK的
  使用jar包:org.nuxeo.common.xmap

  写个小例子
  XML
  <?xml version="1.0" encoding="UTF-8"?>
  <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>

  Books类

  import java.util.ArrayList;
  import java.util.List;
  import org.nuxeo.common.xmap.annotation.XNodeList;
  import org.nuxeo.common.xmap.annotation.XObject;

  @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);
  }
}

  Book类

  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;

  @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;
  }
}

  测试类:


  import java.util.ArrayList;
  import java.util.List;
  import org.nuxeo.common.xmap.XMap;
  public class XmapTest {
  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>();
   System.out.println(xmp.asXmlString(books, "UTF-8", filters, true));

  } catch (Exception e) {
   e.printStackTrace();
  }
}
}

  运行上面这个类就可以生成开头所看见的那个XML的内容

  解析xml

  xmap.register(Books.class);
  URL url = new File("book.xml").toURL();
  InputStream in = new FileInputStream(new File("book.xml"));
  Object[] result = (Object[]) xmap.loadAll(in);
  Books books=(Books)result[0];

  数据就完全autowire by name进入到了bean,某些情况下很好用(xml很整齐,每个节点的内容都一样,固定)

  XML和Java Bean互相转换就此实现。

 两个需要注意的地方:1.Xmap包最新下载地址https://maven.nuxeo.org/用最新版本的就行。运行程序时需要把log4j的日志路径配好,否则会报错。s2Xmap需要依赖一个apache的xercesImpl.jar包下载地址http://download.csdn.net/detail/alice_0812/305407

posted on 2012-05-06 00:59  jdonson  阅读(3409)  评论(1编辑  收藏  举报