- packagetest;
- importjava.util.ArrayList;
- importjava.util.List;
- importorg.nuxeo.common.xmap.annotation.XNode;
- importorg.nuxeo.common.xmap.annotation.XNodeList;
- importorg.nuxeo.common.xmap.annotation.XObject;
- /**
- *Book实体对象,此处用XMap注解
- *@authorAdministrator
- *
- */
- @XObject
- publicclassBook{
- @XNode("title")
- privateStringtitle;
- @XNode("@no")
- privateStringno;
- @XNodeList(value="subject",type=ArrayList.class,componentType=String.class)
- privateList<String>subjects;
- @XNode("price")
- privateintprice;
- publicStringgetTitle(){
- returntitle;
- }
- voidsetTitle(Stringtitle){
- this.title=title;
- publicList<String>getSubjects(){
- returnsubjects;
- voidsetSubjects(List<String>subjects){
- this.subjects=subjects;
- intgetPrice(){
- returnprice;
- voidsetPrice(intprice){
- this.price=price;
- publicStringgetNo(){
- returnno;
- voidsetNo(Stringno){
- this.no=no;
- }
- /**==================================================**/
- packagetest;
- importjava.util.ArrayList;
- importjava.util.List;
- /**
- *图书对象集合类
- *@authorzhouyu
- *
- */
- @XObject(value="books")
- classBooks{
- @XNodeList(value="book",componentType=Book.class)
- privateList<Book>book;
- publicList<Book>getBook(){
- returnbook;
- voidsetBook(List<Book>book){
- this.book=book;
- voidaddBook(Bookb){
- if(book==null){
- book=newArrayList<Book>();
- book.add(b);
- /**==================================================**/
- importorg.nuxeo.common.xmap.XMap;
- importcom.io.IoUtils;
- *从对象到xml转换
- *@authorzhouyu
- classObjectToXmlDemo{
- staticvoidmain(String[]args){
- try{
- //声明一组书的根节点
- Booksbooks=newBooks();
- //声明一本书
- Bookbook=newBook();
- //书的类型
- List<String>sj=newArrayList<String>();
- sj.add("java");
- sj.add("xml");
- //设置书节点的属性
- book.setPrice(10);
- book.setTitle("java是傻逼");
- book.setSubjects(sj);
- book.setNo("1");
- //将书节点添加到跟节点
- books.addBook(book);
- //声明一本书
- Bookbook2=newBook();
- //书的类型
- List<String>sj2=newArrayList<String>();
- sj2.add("C++");
- sj2.add("c");
- //设置书节点的属性
- book2.setPrice(10);
- book2.setTitle("c++程序设计");
- book2.setSubjects(sj2);
- book2.setNo("2");
- //将书节点添加到跟节点
- books.addBook(book2);
- XMapxmp=newXMap();
- xmp.register(Books.class);
- List<String>filters= Stringcontent=xmp.asXmlString(books,"UTF-8",filters,true);
- IoUtils.WriteFile(content,"books.xml");
- }catch(Exceptione){
- e.printStackTrace();
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.InputStream;
- importjava.net.URL;
- importorg.nuxeo.common.xmap.XMap;
- classXmlToObjectDemo{
- voidmain(String[]args){
- try{
- //URLurl=newFile("books.xml").toURL();
- InputStreamin=newFileInputStream(newFile("books.xml"));
- Object[]result=(Object[])xmp.loadAll(in);
- Booksbooks=(Books)result[0];
- for(inti=0;i<books.getBook().size();i++)
- {
- Bookbook=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));
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- <?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
- <books>
- <bookno="1">
- <title>java是傻逼</title>
- <subject>java</subject>
- <subject>xml</subject>
- <price>10</price>
- </book>
- <bookno="2">
- <title>c++程序设计</title>
- <subject>C++</subject>
- <subject>c</subject>
- </books>
本文转自:http://blog.csdn.net/xiaoyu411502/article/details/5566398
原文链接:/xml/298217.html