Now, I have a class DbMO, with field areaCode, phoneNumber and messageType. I have marshalled them to xml successfully 
 
   
      DbMO bean = new DbMO("8888888","4564898","let's go");
      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);
 
 
but failed to unmarshall it from the xml, all the result is null, even none of the set...() methods has been invoked, after the invokation of the constructor.
 
    File file = new File("test.xml");
    Mapping map = new Mapping();
    map.loadMapping("mapping.xml");
    Reader reader = new FileReader(file);
    Unmarshaller um =new Unmarshaller(map);
    DbMO mo = (DbMO)um.unmarshal(com.ecoolstudio.sms.poker.database.DbMO.class, reader);
    System.out.println("-----DbMO---------");
    System.out.println(mo.getAreaCode());
    System.out.println(mo.getPhoneNumber());
    System.out.println(mo.getMessageType());
 
Dramatically, when the unmarshalling pattern as follow was applied, as is recomended on the homepage of castor, the object is loaded successfully.
    File file = new File("test1.xml");
     Mapping map = new Mapping();
     map.loadMapping("message.xml");
     Reader reader = new FileReader(file);
     Unmarshaller um =new Unmarshaller(com.ecoolstudio.sms.poker.database.DbMO.class);
     um.setMapping(map);
     DbMO read = (DbMO)um.unmarshal(reader);
     System.out.println("-----DbMO---------");
     System.out.println(read.getAreaCode());
     System.out.println(read.getPhoneNumber());
     System.out.println(read.getMessageType());
 
It seam that whether unmarshall is successful is determined by the invokation sequence. 
Anyone would like to give me a reason.

Reply via email to