使用SimpleFramework来序列化对象并解析XML

package cn.jn.bq.itext.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.*;
import org.simpleframework.xml.core.Persister;

/**
 * <?xml version="1.0"?>
<template width="1024" height=”768” background=”1.jpg”>
    <logo x=”” y=””/>    
    <items>
        <item x="" y=”” width=”” height=””>
        <text></text>
        <font></font>
        <font_size></font_size>
        <piclink></piclink>
        <link></link>            
        </item>
        <item x="" y=”” width=”” height=””>
        <text></text>
        <font></font>
        <font-size></font-size>
        <piclink></piclink>
        <link></link>            
        </item>
    </items>
    <copywrite x=”” y=””/>
</template>
 * @author grid
 *
 */

class Logo{
    @Attribute
    public int x;
    @Attribute
    public int y;
    public Logo(){}    //注意,这个空的构造函数很重要,否则会报错的,每个子类都是如此
    public Logo(int x,int y){
        this.x = x;
        this.y = y;
    }
    public void setX(int x){
        this.x = x;
    }
    public void setY(int y){
        this.y = y;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
}
@Root
class Item{
    @Attribute
    public int x;
    @Attribute
    public int y;
    @Attribute
    public int width;
    @Attribute
    public int height;
    
    @Element
    public String text;
    @Element
    public String font;
    @Element
    public String font_size;
    @Element
    public String piclink;
    @Element
    public String link;
    
    public Item(){
        
    }
    public Item(int x,int y, int width,int height,String text,String font,String font_size,String piclink,String link){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.text = text;
        this.font = font;
        this.font_size = font_size;
        this.piclink = piclink;
        this.link = link;
    }
    
}
class Copywrite{
    @Attribute
    public int x;
    @Attribute
    public int y;
    public Copywrite(){}
    public Copywrite(int x,int y){
        this.x = x;
        this.y = y;
    }
}
@Root
public class Template {
    public List<Item> items;    
    public Logo logo;    
    public Copywrite copywrite;    
    public int width;    
    public int height;    
    public String background;
    public Template(@ElementList(name="items") List<Item> items,@Element(name="logo") Logo logo,@Element(name="copywrite") Copywrite copywrite,@Attribute(name="width") int width,@Attribute(name="height") int height,@Attribute(name="background") String background ){
        this.items = items;
        this.logo = logo;
        this.copywrite = copywrite;
        this.width = width;
        this.height = height;
        this.background = background;
    }
    
    @ElementList(name="items")
    public List<Item> getItems(){
        if(items== null)
            items = new ArrayList<Item>();
        return this.items;
    }
    @Element(name="logo")
    public Logo getLogo() {
        return logo;
    }

    @Element(name="copywrite")
    public Copywrite getCopywrite() {
        return copywrite;
    }

    @Attribute(name="width")
    public int getWidth() {
        return width;
    }

    @Attribute(name="height")
    public int getHeight() {
        return height;
    }

    @Attribute(name="background")
    public String getBackground() {
        return background;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Logo logo=new Logo(100,200);
        Copywrite copywrite = new Copywrite(500,1000);
        Item item1 = new Item(10,20,30,40,"text1","font","font_size","http://test.com/1xxxx.jpg","http://test.com/1/20000000");
        Item item2 = new Item(100,200,300,400,"text2","font","font_size","http://test.com/2xxxx.jpg","http://test.com/1/3xxxxxx");
        List<Item> items = new ArrayList<Item>();
        items.add(item1);
        items.add(item2);
        Template t = new Template(items,logo,copywrite,200,300,"background.jpg");
        
        Serializer serializer = new Persister();
        File result = new File("/home/grid/taobao/files/template.xml");
        try {
            serializer.write(t, result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        try {
            Template t2 = serializer.read(Template.class,result);
            
            System.out.println(t2.items.get(1).piclink);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值