写一个转换器,应该是类似代码片段的东西
class FieldDtoConvertor implements Converter {
@SuppressWarnings("unchecked")
public boolean canConvert(final Class clazz) {
return clazz.equals(FieldDto.class);
}
public void marshal(final Object value,
final HierarchicalStreamWriter writer,
final MarshallingContext context) {
final FieldDto fieldDto = (FieldDto) value;
writer.addAttribute(fieldDto.getAttributeName(), fieldDto.getAttributeValue());
}
}
在使用 XStream 时,注册转换器
final XStream stream = new XStream(new DomDriver());
stream.registerConverter(new FieldDtoConvertor());