Castor 简单应用 续

 

       上面对不用映射文件的方式做了一个简单的应用,那下面就对应用映射文件的形式做个小实例:

1.标准应用(mapping.xml)<o:p> </o:p>

测试beanStdBean.java

package org.ndot.std;<o:p> </o:p>

public class StdBean {<o:p></o:p>

  private int num;<o:p></o:p>

  private String stdName;<o:p></o:p>

  private boolean isNew = false;<o:p></o:p>

  public boolean getIsNew() {<o:p></o:p>

     return isNew;<o:p></o:p>

  }<o:p></o:p>

  public void setIsNew(boolean isNew) {<o:p></o:p>

     this.isNew = isNew;<o:p></o:p>

  }<o:p></o:p>

  public int getNum() {<o:p></o:p>

     return num;<o:p></o:p>

  }<o:p></o:p>

  public void setNum(int num) {<o:p></o:p>

     this.num = num;<o:p></o:p>

  }<o:p></o:p>

  public String getStdName() {<o:p></o:p>

     return stdName;<o:p></o:p>

  }<o:p></o:p>

  public void setStdName(String stdName) {<o:p></o:p>

     this.stdName = stdName;<o:p></o:p>

  }<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

影射文件 mapping.xml<o:p></o:p>

<o:p> </o:p>

<!DOCTYPE databases PUBLIC<o:p></o:p>

  "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"<o:p></o:p>

  "http://castor.exolab.org/mapping.dtd"><o:p></o:p>

<mapping><o:p></o:p>

  <description>Basic mapping example</description><o:p></o:p>

  <class name="org.ndot.std.StdBean" auto-complete="true"><o:p></o:p>

    <map-to xml="MyInfo"/><o:p></o:p>

    <field name="isNew" ><o:p></o:p>

      <bind-xml name="isNew" node="attribute"/><o:p></o:p>

    </field><o:p></o:p>

    <field name="num"><o:p></o:p>

      <bind-xml name="num" node="attribute"/><o:p></o:p>

    </field><o:p></o:p>

    <field name="stdName"><o:p></o:p>

      <bind-xml name="stdName" node="attribute"/><o:p></o:p>

    </field><o:p></o:p>

  </class><o:p></o:p>

</mapping><o:p></o:p>

<o:p> </o:p>

测试类:TestMain.java<o:p></o:p>

<o:p> </o:p>

package org.ndot.std;<o:p></o:p>

import java.io.*;<o:p></o:p>

import org.exolab.castor.xml.*;<o:p></o:p>

import org.exolab.castor.mapping.*;<o:p></o:p>

public class TestMain {<o:p></o:p>

  public static void main(String[] argv) {<o:p></o:p>

     // build a test bean<o:p></o:p>

     StdBean bean = new StdBean();<o:p></o:p>

     bean.setIsNew(true);<o:p></o:p>

     bean.setNum(22);<o:p></o:p>

     bean.setStdName("NDot");<o:p></o:p>

     try {<o:p></o:p>

         // write it out as XML<o:p></o:p>

         Mapping map = new Mapping();<o:p></o:p>

         map.loadMapping("mapping.xml");<o:p></o:p>

<o:p> </o:p>

         File file = new File("MyInfo.xml");<o:p></o:p>

         Writer writer = new FileWriter(file);<o:p></o:p>

         Marshaller marshaller = new Marshaller(writer);<o:p></o:p>

         marshaller.setMapping(map);<o:p></o:p>

         marshaller.marshal(bean);<o:p></o:p>

<o:p> </o:p>

         // now restore the value and list what we get<o:p></o:p>

         Reader reader = new FileReader(file);<o:p></o:p>

         Unmarshaller unmarshaller = new Unmarshaller(map);<o:p></o:p>

         StdBean read = (StdBean) unmarshaller.unmarshal(reader);<o:p></o:p>

         System.out.println("Num is :" + read.getNum());<o:p></o:p>

     } catch (Exception ex) {<o:p></o:p>

         ex.printStackTrace(System.err);<o:p></o:p>

     }<o:p></o:p>

  }<o:p></o:p>

}<o:p></o:p>

控制台输出:Num is :22<o:p></o:p>

生成XML文档:MyInfo.xml<o:p></o:p>

<?xml version="1.0" encoding="UTF-8"?><o:p></o:p>

<MyInfo isNew="true" num="22" stdName="NDot" />

好了关于Castor的java对象到xml的绑定的小实例就说到这里,上面涉及的内容简单肤浅,只是给没有接触过Castor的同仁作个小参考而已,敬请大家多多指教!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值