这篇文章主要介绍了怎么在Java中将Bean与xml进行转换,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:
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];
到此这篇关于怎么在Java中将Bean与xml进行转换的文章就介绍到这了,更多相关内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。