码迷,mamicode.com
首页 > 其他好文 > 详细

xstream的Converter的使用

时间:2015-04-24 19:29:32      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:xstream converter

在网上看了下,有关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) {
                    super(mapper);
          }

          @Override
          public boolean canConvert(Class type) {
                   return type.equals(ArrayList.class);
           }

           @Override
          public void marshal(Object source, HierarchicalStreamWriter writer,
                       MarshallingContext context) {
                 List<String> list = (List<String>)source;  
                 ListIterator<String> iter = list.listIterator() ;
                 String value = null ;
                 while(iter.hasNext()){
                       value = iter.next() ;
                       ExtendedHierarchicalStreamWriterHelper.startNode(writer,"include",List.class); 
                       writer.setValue(value); 
                       writer.endNode(); 
            }
  }

  @Override
  public Object unmarshal(HierarchicalStreamReader reader,
        UnmarshallingContext context) {
                String nodename = reader.getNodeName() ;
                //System.out.println(nodename);
                List<String> list = new ArrayList<String>();
                while(reader.hasMoreChildren()){
                         reader.moveDown();
                      nodename = reader.getNodeName() ;
                      //System.out.println(nodename+"-->"+reader.getValue());
                      list.add(reader.getValue()) ;
                      reader.moveUp();
                 }
                 return list;
        }
 }

使用方式:

   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)) ;


本文出自 “因简单而自在” 博客,请务必保留此出处http://dengshuangfu.blog.51cto.com/8794650/1638068

xstream的Converter的使用

标签:xstream converter

原文地址:http://dengshuangfu.blog.51cto.com/8794650/1638068

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!