在网上看了下,有关xstream的Converter的使用的例子很少,这里做个简单的应用举例
文件格式如下:
<list> <mapper neType="2g"> <sqlByType></sqlByType> <sqlById></sqlById> <types> <include>BTS</include> </types> </mapper> <mapper neType="3g"> <types> <include>NODEB</include> </types> </mapper> </list> |
实体类定义如下:
@XStreamAlias("mapper") public class NeTypeMapper { @XStreamAsAttribute public String neType ; @XmlElement public String sqlByType ; @XmlElement public String sqlById ; @XStreamAlias("types") public List<String> includeTypes = new ArrayList<String>(); } |
转换器:
static class ListConverter extends AbstractCollectionConverter { public ListConverter(Mapper mapper) { @Override @Override @Override |
使用方式:
XStream xs = new XStream() ; xs.processAnnotations(NeTypeMapper.class); xs.registerLocalConverter(NeTypeMapper.class, "includeTypes", new ListConverter( new DefaultMapper(XStream.class.getClassLoader())) ); File file = new File(filePath) ; //filePath:文件的路径 try { List<NeTypeMapper> list = (List<NeTypeMapper>)xs .fromXML(new FileInputStream(file)) ; |