XStream:具有属性和文本节点的节点? [英] XStream : node with attributes and text node?
本文介绍了XStream:具有属性和文本节点的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用XStream将对象序列化为此表单的XML。
I would like to serialize an object to an XML of this form with XStream.
<node att="value">text</node>
节点的值( text )是序列化的字段对象,以及 att 属性。如果没有为这个对象编写转换器,这可能吗?
The value of the node (text) is a field on the serialized object, as well as the att attribute. Is this possible without writing a converter for this object?
谢谢!
推荐答案
写一个转换器,它应该类似于代码片段
write a convertor, it should be something similar to the code snippet
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时,注册转换器
And while using XStream,register the convertor
final XStream stream = new XStream(new DomDriver());
stream.registerConverter(new FieldDtoConvertor());
这篇关于XStream:具有属性和文本节点的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文