最近在做的项目,在各个平台之间需要大量传输数据,且结构比较复杂,同时需要接近完全的配置化。

所以:想着各个系统之间,使用java bean来填充值,然后,转成xml进行传输,在服务器端再进行将xml转换成java bean,来处理的方式。

 

/**
 * xml 与 java bean 相互转换 单元 测试 
 * @author zhaoguoli
 * @version v1.0 2013-2-4 create
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.nuxeo.common.xmap.XMap;


public class XmapTest {
	
	@Test
    public void javaToXml(){
        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);
            
   

            System.out.println(xmp.toXML(books));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	
	@Test
	public void xmlToJava() throws Exception{
		XMap xmap = new XMap();
		xmap.register(Books.class);
        InputStream in = new FileInputStream(new File("G:\\ers\\xxt_ers\\WEB-INF\\src\\cn\\xxt\\xmlbean\\books.xml"));
        Object result = xmap.load(in);
        
        System.out.println(result);
		Books books=(Books)result;		
		System.out.println(books);

	}
}

 

 

/**
 * 图书
 * @author zhaoguoli
 * @version v1.0 2013-2-4 create
 */
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
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;
	}
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}

 

/**
 * 图书列表
 * @author zhaoguoli
 * @version v1.0 2013-2-4 create
 */
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
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);
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
    	// TODO Auto-generated method stub
    	return ToStringBuilder.reflectionToString(this);
    }
}

 

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <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>
</root>