Xstream官网:http://x-stream.github.io/alias-tutorial.html
--------------------------------------------------------- <XXX id="1234">哈哈</XXX> 如这种,bean定义为 public class XXX{ private String content;
private String id; } 转换时,这样 xstream.alias("XXX", XXX.class); xstream.aliasAttribute(XXX.class, "id", "id"); //属性 是可以转换属性id的,但是文本内容怎么搞呢? --------------------------------------------------------- 这样,加一行注解就行了 @XStreamConverter(value=ToAttributedValueConverter.class, strings={"content"}) public class XXX{ private String content;
private String id; } 另外还得 xstream.autodetectAnnotations(true); //读取类中的注解 才行。 ----------------------------------------------------------- 另外的方法是自己写转换类,参考:
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()); } } final XStream stream = new XStream(new DomDriver()); stream.registerConverter(new FieldDtoConvertor());
|