如果你不知道XStream是什么东东,请看我以前的一篇文章:
http://fuliang.iteye.com/blog/379429
今天用XStream序列化对象,其中有Map类型的字段,结果不让人满意:
<ExtractResult>
<dataRecords>
<DataRecord>
<properties>
<property>
<string>region</string>
<string>非洲中东</string>
</property>
<property>
<string>routeReference</string>
<string>首都机场集合,搭乘土耳其航空直飞伊斯坦布尔,夜宿机上。</string>
</property>
<property>
<string>routeDays</string>
<string>10</string>
</property>
<property>
<string>price</string>
<string>¥13500起</string>
</property>
<property>
<string>subject</string>
<string>常规</string>
</property>
<property>
<string>dayOfBookingExpired</string>
<string>2010-02-01</string>
</property>
<property>
<string>departure</string>
<string>北京</string>
</property>
<property>
<string>dayOfDeparture</string>
<string>2010-02-10</string>
</property>
<property>
<string>title</string>
<string>[团队游]埃及土耳其10日千年文明之旅(春节)</string>
</property>
</properties>
</DataRecord>
</dataRecords>
</ExtractResult>
其中的key和value被序列化成:
<string>region</string>
<string>非洲中东</string>
我比较想要的结果是
<property key="region" value="非洲中东"/>
查看了XStream的文档,可以通过自定义Converter来转换,经过尝试,只需要很简单的步骤就可以完成:
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
/**
* @author fuliang
*
*/
public class MapCustomConverter extends AbstractCollectionConverter {
/**
* @param mapper
*/
public MapCustomConverter(Mapper mapper) {
super(mapper);
}
public boolean canConvert(Class type) {
return type.equals(HashMap.class)
|| type.equals(Hashtable.class)
|| type.getName().equals("java.util.LinkedHashMap")
|| type.getName().equals("sun.font.AttributeMap") // Used by java.awt.Font in JDK 6
;
}
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
Map map = (Map) source;
for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
Entry entry = (Entry) iterator.next();
ExtendedHierarchicalStreamWriterHelper.startNode(writer, "property", Entry.class);
writer.addAttribute("key", entry.getKey().toString());
writer.addAttribute("value", entry.getValue().toString());
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map map = (Map) createCollection(context.getRequiredType());
populateMap(reader, context, map);
return map;
}
protected void populateMap(HierarchicalStreamReader reader, UnmarshallingContext context, Map map) {
while (reader.hasMoreChildren()) {
reader.moveDown();
Object key = reader.getAttribute("key");
Object value = reader.getAttribute("value");
map.put(key, value);
reader.moveUp();
}
}
}
然后这么使用:
XStream xStream = new XStream();
xStream.registerConverter(new MapCustomConverter(new DefaultMapper(XmlOutputFormatter.class.getClassLoader())));
xStream.alias("DataRecord", ExtractDataRecord.class);
xStream.alias("ExtractResult", ExtractResult.class);
xStream.alias("property", Entry.class);
return xStream.toXML(extractResult);
序列化后的结果很漂亮:
<ExtractResult>
<dataRecords>
<DataRecord>
<properties>
<property key="region" value="港澳"/>
<property key="routeReference" value="搭乘国际航班直飞桃园国际机场,办理相关手续后,前往用晚餐。前往酒店入住休息。"/>
<property key="routeDays" value="8"/>
<property key="price" value="¥5680起"/>
<property key="subject" value="常规"/>
<property key="dayOfBookingExpired" value="2010-01-15"/>
<property key="departure" value="北京"/>
<property key="dayOfDeparture" value="2010-01-25"/>
<property key="title" value="[团队游]台湾8日宝岛乡情之旅"/>
</properties>
</DataRecord>
</dataRecords>
</ExtractResult>
分享到:
相关推荐
使用XStream序列化/反序列化对象
XStream 用法详解 XML 转换为 java 实体类 所需要的 xstream-1.4.3-sources.jar jar包 具体实现请参考 http://blog.csdn.net/morning99/article/details/28875159
xstream_1.4.11.1.jar 是list转化为xml必须的jar包,通过下载导入可以正常使用
XStream 是一个开源项目,一套简单实用的类库,用于序列化对象与 XML 对象之间的相互转换。将 XML 文件内容解析为一个对象或将一个对象序列化为 XML 文件。
xstream-all-1.4.7.jar;xstream-all-1.4.7.jar;xstream-all-1.4.7.jar;
使用 XStream 不用任何映射就能实现多数 Java 对象的序列化。在生成的 XML 中对象名变成了元素名,类中的字符串组成了 XML 中的元素内容。使用 XStream 序列化的类不需要实现 Serializable 接口。XStream 是一种序列...
xstream-1.1.3正反序列化JAVA对象 xstream-1.1.3.jar 使用xstream-1.1.3.jar 进行序列化 JAVA 对象的示例三个
标签:activemq-transport-xstream-1.3.jar.zip,activemq,transport,xstream,1.3,jar.zip包下载,依赖包
标签:activemq-transport-xstream-1.4.jar.zip,activemq,transport,xstream,1.4,jar.zip包下载,依赖包
标签:activemq-transport-xstream-1.5.jar.zip,activemq,transport,xstream,1.5,jar.zip包下载,依赖包
使用 XStream 把 Java 对象序列化为 XML
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter path : /.../... line number : 1 version : 1.4.7 ------------------------------- 该包解决了这个bug
XStream是一个简单的基于Java库,Java对象序列化到XML,反之亦然(即:可以轻易的将Java对象和xml文档相互转换)。
最齐全的xstream1.4.8.jar 最齐全的xstream1.4.8.jar 最齐全的xstream1.4.8.jar 最齐全的xstream1.4.8.jar 最齐全的xstream1.4.8.jar 最齐全的xstream1.4.8.jar 最齐全的xstream1.4.8.jar 最齐全的xstream1.4.8.jar ...
Xstream 是 Java 类库,用来将对象序列化成 XML (JSON) 或反序列化为对象。XStream 是一款开源软件,允许在 BSD 许可证的许可下分发。 0x01 漏洞描述 Xstream上次对CVE-2020-26217处理并不彻底,虽然通过黑名单...
xstream-1.4.8-sources.jar
Java对象转换成XML字符串 将List集合转换成xml文档 Map集合转换xml文档 用InputStream将XML文档转换成java对象
标签:activemq-transport-xstream-2.1.jar.zip,activemq,transport,xstream,2.1,jar.zip包下载,依赖包
标签:activemq-transport-xstream-1.2.jar.zip,activemq,transport,xstream,1.2,jar.zip包下载,依赖包
标签:activemq-transport-xstream-2.0.jar.zip,activemq,transport,xstream,2.0,jar.zip包下载,依赖包