用Castor进行java到xml的绑定

从Java类到XML的过程称为“串行化”,反之的过程成为“反串行化”。

Castor是绑定Java类和XML文档的框架名称。 

比如我有一个Inventory类,用来描述产品目录:

import  java.util. * ;

public   class  Inventory {
    
private  String company;
    
private  Vector products  =   new  Vector();
    
    
public  Inventory() {}
    
    
public   void  setCompany(String company) {
        
this .company  =  company;
    }
 
    
public  String getCompany() {
        
return   this .company;
    }
    
    
public   void  addProduct(Product product) {
        products.add(product);
    }
    
    
public  Vector getProducts() {
        
return   this .products;
    }
}

还有一个Product类,用来描述产品信息:

public   class  Product {
    
private  String name;
    
private  String description;
    
    
public  Product() {}
    
    
public   void  setName(String name) {
        
this .name  =  name;
    }
    
    
public  String getName() {
        
return   this .name;
    }
    
    
public   void  setDescription(String description) {
        
this .description  =  description;
    }
    
    
public  String getDescription() {
        
return   this .description;
    }
    
    
public  String toString() {
        
return   " name:  "   +  name  +   " , description:  "   +  description;
    }

}

 

下面用InventoryManager类进行串行化/反串行化

import  org.exolab.castor.mapping. * ;
import  org.exolab.castor.types. * ;
import  org.exolab.castor.xml. * ;
import  java.io. * ;
import  java.util. * ;

public   class  InventoryManager {

    
/**
     * 
@param  args
     
*/
    
public   static   void  main(String[] args) {
        
new  InventoryManager();

    }
    
    
public  InventoryManager() {
        Inventory inventory 
=   new  Inventory();
        inventory.setCompany(
" ibm " );
        
        Product product 
=   new  Product();
        product.setName(
" nokia " );
        product.setDescription(
" handphone " );
        
        inventory.addProduct(product);
        
        
try  {
            
//  串行化,生成inventory.xml文件
            Writer writer  =   new  FileWriter( " inventory.xml " );            
            Marshaller.marshal(inventory, writer);
            
            
//  反串行化,读取inventory.xml文件,生成inventory对象
            Unmarshaller unmarshaller  =   new  Unmarshaller(Inventory. class );
            
            inventory 
=  (Inventory)unmarshaller.unmarshal( new  FileReader( " inventory.xml " ));
            
            
//  打印对象信息
            System.out.println(inventory.getCompany());
            
            Vector products 
=  inventory.getProducts();            
            
for  ( int  i  =   0 ; i  <  products.size(); i ++ ) {
                product 
=  (Product)products.get(i);
                System.out.println(product);
            }
            
        } 
catch (IOException ex) {
            System.out.println(ex);
        } 
catch  (MarshalException ex) {
            System.out.println(ex);
        } 
catch  (ValidationException ex) {
            System.out.println(ex);
        }
        
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值