服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

castor -- 将java对象和xml自动绑定的程序


  1. castor是什么
  
  castor是一种将java对象和xml自动绑定的开源软件. 它可以在java对象,xml文本,sql数据表以及ldap目录之间绑定.
  
  ²网址: http://www.castor.org/
  
  2. castor使用
  
  ²java对象指的是具有setx和getx方法的javabean,将castor用于javabean具体使用方法如下:
  
  ø缺省用法: 缺省用法指的是没有使用映射格式描述文件时的用法
  
  import java.io.*;
  
  import org.exolab.castor.xml.*;
  
  public class test
  
  {
  
  public static void main(string[] argv) {
  
  // build a test bean
  
  flightbean bean = new flightbean();
  
  bean.setcarrier("ar");
  
  bean.setnumber(426);
  
  bean.setdeparturetime("6:23a");
  
  bean.setarrivaltime("8:42a");
  
  try {
  
  // write it out as xml
  
  file file = new file("test.xml");
  
  writer writer = new filewriter(file);
  
  marshaller.marshal(bean, writer);
  
  // now restore the value and list what we get
  
  reader reader = new filereader(file);
  
  flightbean read = (flightbean)
  
  unmarshaller.unmarshal(flightbean.class, reader);
  
  system.out.println("flight " + read.getcarrier() +
  
  read.getnumber() + " departing at " +
  
  read.getdeparturetime() +
  
  " and arriving at " + read.getarrivaltime());
  
  } catch (ioexception ex) {
  
  ex.printstacktrace(system.err);
  
  } catch (marshalexception ex) {
  
  ex.printstacktrace(system.err);
  
  } catch (validationexception ex) {
  
  ex.printstacktrace(system.err);
  
  }
  
  }
  
  }
  
  ø 标准用法:
  
  import java.io.*;
  
  import org.exolab.castor.xml.*;
  
  import org.exolab.castor.mapping.*;
  
  
  public class test
  
  {
  
  public static void main(string[] argv) {
  
  // build a test bean
  
  flightbean bean = new flightbean();
  
  bean.setcarrier("ar");
  
  bean.setnumber(426);
  
  bean.setdeparturetime("6:23a");
  
  bean.setarrivaltime("8:42a");
  
  try {
  
  // write it out as xml
  
  mapping map=new mapping();
  
  map.loadmapping("mapping.xml");
  
  file file = new file("test.xml");
  
  writer writer = new filewriter(file);
  
  marshaller marshaller =new marshaller(writer);
  
  marshaller.setmapping(map);
  
  marshaller.marshal(bean);
  
  // now restore the value and list what we get
  
  reader reader = new filereader(file);
  
  unmarshaller unmarshaller = new unmarshaller(map);
  
  flightbean read = (flightbean)unmarshaller.unmarshal(reader);
  
  system.out.println("flight " + read.getcarrier() +
  
  read.getnumber() + " departing at " +
  
  read.getdeparturetime() +" and arriving at " + read.getarrivaltime());
  
  } catch (ioexception ex) {
  
  ex.printstacktrace(system.err);
  
  } catch (marshalexception ex) {
  
  ex.printstacktrace(system.err);
  
  } catch (validationexception ex) {
  
  ex.printstacktrace(system.err);
  
  }
  
  catch (mappingexception ex) {
  
  ex.printstacktrace(system.err);
  
  }
  
  }
  
  }
  
  ø :缺省用法生成的xml文件如下:
  
  <?xml version="1.0" encoding="utf-8"?>
  
  <arrival-time>8:42a</arrival-time>
  <departure-time>6:23</departure-time>
  <carrier>ar</carrier>
  
  也就是说
  •对于具有基本类型值的属性创建元素的一个属性(本例中只有 number 属性通过 getnumber() 方法公开为 int 值)。
  
  •对于每个具有对象类型值的属性创建根元素的一个子元素(本例中的所有其他属性,因为它们是字符串)。
  
  ø标准用法生成的xml文件如下:
  <?xml version="1.0" encoding="utf-8"?>
  <flight carrier="ar" depart="6:23a" arrive="8:42a" number="426"/>
  这一般是我们期望的,只所以生成我们所期望的格式,是因为我们使用了映射格式描述文件。它也是xml文件:
  
  <!doctype databases public "-//exolab/castor mapping dtd version 1.0//en" "http://castor.exolab.org/mapping.dtd"><mapping> <description>basic mapping example</description> <class name="castortest.flightbean" auto-complete="true">  <map-to xml="flight"/>  <field name="carrier">   <bind-xml name="carrier" node="attribute"/>  </field>  <field name="departuretime">   <bind-xml name="depart" node="attribute"/>  </field>  <field name="arrivaltime">   <bind-xml name="arrive" node="attribute"/>  </field> </class></mapping>•     注意class标签里name应该是带有包名的javabean类全名
  •map-to xml="flight"指xml文件的根节点,区分大小写
  
  •auto-complete=”false”或没有auto-complete属性时number属性会没有

扫描关注微信公众号