以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  使用castor时只能写不能读(求助)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=27916)


--  作者:v_fire
--  发布时间:2/28/2006 8:25:00 PM

--  使用castor时只能写不能读(求助)
使用castor进行数据绑定时,只能创建xml文件,不能从xml文件中读取数据。总是抛出marshallException异常:org.xml.sax.SAXException: unable to instantiate Security; java.lang.InstantiationException: Security{file: [not available]; line: 2; column: 11}
那位能帮忙看看。
/*这是调用程序,下面还有一个Security类是对应xml文件的*/
import org.exolab.castor.xml.*;
import java.io.File;
import java.io.*;
public class CastorTest {
public static void main(String[] args) {
  Security sc=null;
  Security cb=new Security("dfas","dfas","adf","dfsad");
  try{
      File file = new File("xcbb.xml");
      Writer writer = new FileWriter(file);
      Marshaller.marshal(cb, writer);
      System.out.println(""+Security.class);
      writer.close();
      /*以上可以获得成功,以下读不成功抛出MarshalException*/
      Reader rd = new FileReader(file);
      System.out.println("saldfjaslkdfj");
      sc = (Security) Unmarshaller.unmarshal(Security.class,rd);
      System.out.println(sc.getAlarmphone());
      rd.close();
  }catch(IOException ex){
      System.out.println("Lost in io");
  }catch(MarshalException ex){
      System.out.println("Lost in marshal::"+ex.toString()+"\n");
      //ex.printStackTrace();
  }catch(ValidationException ex){
      System.out.println("Lost in valid");
  }
  
                    }

}
/*这是我定义的一个关于报警设置的类*/
public class Security {
    
    String alarmphone;
    String alarmsms;
    String startupdelay;
    String delayunit;
    
    //构造函数
    Security(String ph,String sms,String de,String un){
     alarmphone   = ph;
     alarmsms     = sms;
     startupdelay = de;
     delayunit    = un;
    }
    
    //获取报警电话
    public String getAlarmphone(){
     return alarmphone;
    }

    //设置报警电话
    public void setAlarmphone(String ph){
     alarmphone = ph;
    }
    
    //获取报警短信号码
    public String getAlarmsms(){
     return alarmsms;
    }
    
    //设置报警短信号码
    public void setAlarmsms(String sms){
     alarmsms = sms;
    }
    
    //获取延迟时间
    public String getStartupdelay(){
     return startupdelay;
    }
    
    //设置延迟时间
    public void setStartupdelay(String de){
     startupdelay = de;
    }
    
    //获取延迟时间单位
    public String getDelayunit(){
     return delayunit;
    }
    
    //设置报警短信号码
    public void setDelayunit(String un){
     delayunit = un;
    }
}



--  作者:datoubo
--  发布时间:11/23/2006 8:46:00 AM

--  
在Security加上一个不带参数的构造方法.
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms