首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

运用X地图将xml数据转换成javabean

2012-08-31 
运用Xmap将xml数据转换成javabeanxml和javabean 的转换有很多工具可以完成,比如常用的xstream,json-lib。。。

运用Xmap将xml数据转换成javabean

xml和javabean 的转换有很多工具可以完成,比如常用的xstream,json-lib。。。

这里要了解的是Xmap这个在Nuxeo开源ECM开源框架下的org.nuxeo.common.xmap的jar包工具。

最新版本jar包可以在这里下载 https://maven.nuxeo.org 搜索org.nuxeo.common便可以找到 。说明文档可以到这里查看 http://community.nuxeo.com/api/nuxeo/5.1/javadoc/org/nuxeo/common/

?

通过注解就直接得到一个Java Bean。

下面贴一个参考的例子:

books.xml

<?xml version="1.0" encoding="UTF-8"?><books>    <book no="1">      <title>java是傻逼</title>      <subjects>        <string>java</string>        <string>xml</string>      </subjects>      <price>10</price>    </book>    <book no="2">      <title>c++程序设计</title>      <subjects>        <string>C++</string>        <string>c</string>      </subjects>      <price>10</price>    </book></books>
?

?

Books.java

package com.hand.entity;import java.util.ArrayList;import java.util.List;import org.nuxeo.common.xmap.annotation.XNodeList;import org.nuxeo.common.xmap.annotation.XObject;@XObject("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.java

package com.hand.entity;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;@XObjectpublic 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 String getNo() {return no;}public void setNo(String no) {this.no = no;}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;}}

?

?Test.java

public class Test {public static void main(String[] args){Books books=new Books();Book book1=new Book();List< String> sj=new ArrayList< String>();sj.add("java");sj.add("xml");book1.setPrice(10);book1.setTitle("java是傻逼");book1.setSubjects(sj);book1.setNo("1");books.addBook(book1);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);//xml转换javabean                InputStream in = null;Object[] result;try {in = new FileInputStream(new File("xml文件路径"));result = (Object[]) xmp.loadAll(in);Books books=(Books)result[0];System.out.println(books.getBook().get(0).getTitle());} catch (FileNotFoundException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}                //javabean转换xml                String xmlStr = null;try {xmlStr = xmp.toXML(books);} catch (ParserConfigurationException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}?}附件里面有需要的jar包

热点排行