从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;
}
}
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;
}
}
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);
}
}
}
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);
}
}
}