xstream反序列化问题

时间:2013-04-10 11:24:34

标签: xml xstream xml-deserialization

尝试对以下XML进行反序列化,其中“day”& “fulldob”是对象 具有自定义逻辑的各个转换器的类型。一旦反序列化&什么时候到XML 方法应用于对象,“day”&的值。 “fulldob”失踪了。意义,日和fulldob没有正确地反序列化。有人可以帮忙吗?

** * ** * ** * 的* 输入XML * ** * ** *

 <primarydob>
   <month>05</month>
   <day>13</day>
   <year>1982</year>
  <fulldob>19820513</fulldob>
 </primarydob>

** * * 来自对象的XML(在反序列化上述XML之后) * ** * ** *

 <primarydob>
   <month>05</month>
   <day/>
   <year>1982</year>
   <fulldob/>
 </primarydob>

这是“primarydob”的对象

@XStreamAlias("dob")   
public class DOBData {

  @XStreamAlias("month")
  private String month;

  @XStreamAlias("day")
  private DOBDay dobDay = new DOBDay();

  @XStreamAlias("year")
  private String year;

  @XStreamAlias("fulldob")
  private FullDOB fulldob = new FullDOB();

  getters/setters
}

这是“fulldob”的对象

@XStreamAlias("fulldobdata")
@XStreamConverter(FullDOBDayMaskingConverter.class)
public class FullDOB {

  @XStreamAlias("fulldob")
  private String fullDOB;

  public String getFullDOB() {
      return fullDOB;
  }

  public void setFullDOB(String fullDOB) {

      this.fullDOB = fullDOB;
  }
}

这是“日”的对象

@XStreamAlias("dobday")
@XStreamConverter(DOBDayMaskingConverter.class)
public class DOBDay {

  @XStreamAlias("day")
  private String day;

  public String getDay() {
         return day;
  }
  public void setDay(String day) {
         this.day = day;
  }
}

谢谢,

1 个答案:

答案 0 :(得分:0)

这是DOBDayConverter代码:

public class DOBDayConverter extends ReflectionConverter {
    public DOBDayConverter(XStream xstream) {
        super(xstream.getMapper(), xstream.getReflectionProvider());
    }

    @Override
    public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
        DOBDay d = (DOBDay) obj;
        try {
            writer.setValue(d.getDay());
        } catch (Exception e) {
        }   
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
        DOBDay day = new DOBDay();

        day.setDay(reader.getValue());

        return day;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public boolean canConvert(Class clazz) {
        return clazz.equals(DOBDay.class); 
    }

}

这是FullDOBConverter代码:

public class FullDOBConverter extends ReflectionConverter {
    public FullDOBConverter(XStream xstream) {
        super(xstream.getMapper(), xstream.getReflectionProvider());
    }

    @Override
    public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
        FullDOB fd = (FullDOB) obj;
        try {
            writer.setValue(fd.getFullDOB());
        } catch (Exception e) {
        }   
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
        FullDOB fd = new FullDOB();

        fd.setFullDOB(reader.getValue());

        return fd;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public boolean canConvert(Class clazz) {
        return clazz.equals(FullDOB.class); 
    }

}

不要忘记注册转换器并处理注释:

XStream xs = new XStream();
xs.processAnnotations(DOBData.class);
xs.processAnnotations(FullDOB.class);
xs.processAnnotations(DOBDay.class);
xs.registerConverter(new DOBDayConverter(xs));
xs.registerConverter(new FullDOBConverter(xs));