0

尝试反序列化以下 XML,其中“day”和“fulldob”是对象类型,它们具有用于自定义逻辑的单独转换器。一旦反序列化 & 当 toXML 方法应用于对象时,“day”和“fulldob”的值就会丢失。意思是,day & fulldob 没有正确反序列化。有人可以帮忙吗?

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

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

** * *来自 Object 的 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;
  }
}

谢谢,

4

1 回答 1

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));
于 2013-04-15T18:32:23.187 回答