使用轻量级的XStream可以很方便的将对象序列化为XML
此外,XStream还可自定义序列化和反序列的转换器(Converter)
下面的转换器可将Map/List序列化,或将其他POJO的序列化XML反转为Map/List
直接上代码 PojoMapConverter.java
package net.cnlab.vo.services.xstream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class PojoMapConverter implements Converter {
public PojoMapConverter() {
super();
}
public boolean canConvert(Class clazz) {
String classname = clazz.getName();
if (classname.indexOf("Map") >= 0
|| classname.indexOf("List") >= 0
|| classname.indexOf("Bean") >= 0)
return true;
else
return false;
}
public void marshal(Object value, HierarchicalStreamWriter writer,
MarshallingContext context) {
map2xml(value, writer, context);
}
protected void map2xml(Object value, HierarchicalStreamWriter writer,
MarshallingContext context) {
boolean bMap = true;
String classname = value.getClass().getName();
bMap = (classname.indexOf("List") < 0);
Map<String, Object> map;
List<Object> list;
String key;
Object subvalue;
if (bMap) {
map = (Map<String, Object>) value;
for (Iterator<Entry<String, Object>> iterator = map.entrySet()
.iterator(); iterator.hasNext();) {
Entry<String, Object> entry = (Entry<String, Object>) iterator
.next();
key = (String) entry.getKey();
subvalue = entry.getValue();
writer.startNode(key);
if (subvalue.getClass().getName().indexOf("String") >= 0) {
writer.setValue((String) subvalue);
} else {
map2xml(subvalue, writer, context);
}
writer.endNode();
}
} else {
list = (List<Object>) value;
for (Object subval : list) {
subvalue = subval;
writer.startNode("child");
if (subvalue.getClass().getName().indexOf("String") >= 0) {
writer.setValue((String) subvalue);
} else {
map2xml(subvalue, writer, context);
}
writer.endNode();
}
}
}
public Map<String, Object> unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
Map<String, Object> map = (Map<String, Object>) populateMap(reader,
context);
return map;
}
protected Object populateMap(HierarchicalStreamReader reader,
UnmarshallingContext context) {
boolean bMap = true;
Map<String, Object> map = new HashMap<String, Object>();
List<Object> list = new ArrayList<Object>();
while (reader.hasMoreChildren()) {
reader.moveDown();
String key = reader.getNodeName();
Object value = null;
if (reader.hasMoreChildren()) {
value = populateMap(reader, context);
} else {
value = reader.getValue();
}
if (bMap) {
if (map.containsKey(key)) {
// convert to list
bMap = false;
Iterator<Entry<String, Object>> iter = map.entrySet()
.iterator();
while (iter.hasNext())
list.add(iter.next().getValue());
// insert into list
list.add(value);
} else {
// insert into map
map.put(key, value);
}
} else {
// insert into list
list.add(value);
}
reader.moveUp();
}
if (bMap)
return map;
else
return list;
}
}
通过XStream中注册该转换器,即可用toXML和fromXML转换
使用示例,将xml转为Map/List,然后再转换为xml
public static void main(String[] args) {
String xml = "<map><stars><star><id>1</id><name>littleflower</name></star><star><id>2</id><name>littleyellow</name></star></stars> <filename>cnlab</filename> <ra>147.0</ra> <dec>0.0</dec> <plate>0.0</plate> <mdj>0.0</mdj> </map> ";
XStream xstream = new XStream(new DomDriver());
xstream.registerConverter(new PojoMapConverter());
Map<String, Object> map = (Map<String, Object>)xstream.fromXML(xml);
System.out.println(map);
xml = xstream.toXML(map);
System.out.println(xml);
System.out.println((Map<String, Object>)xstream.fromXML(xml));