利用XStream将List<String>转为xml后指定String的别名

前端之家收集整理的这篇文章主要介绍了利用XStream将List<String>转为xml后指定String的别名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class Test {  
    public List<String> tags = new ArrayList<String>();  
    public List<String> notags = new ArrayList<String>();  
    public Test(String tag,String tag2) {  
        tags.add(tag); tags.add(tag2);  
        notags.add(tag); notags.add(tag2);  
    } 
    public static void main(String[] args) {  
        Test test = new Test("foo","bar");  
        XStream xstream = new XStream();  
  
        ClassAliasingMapper mapper = new ClassAliasingMapper(xstream.getMapper());  
        mapper.addClassAlias("tag",String.class);  
        xstream.registerLocalConverter(Test.class,"tags",new CollectionConverter(mapper));  
  
        System.out.println(xstream.toXML(test));  
    }  
}

结果:

<tags>  
   <tag>foo</tag>  
   <tag>bar</tag>  
 </tags>  
 <notags>  
   <string>foo</string>  
   <string>bar</string>  
 </notags>  
原文链接:https://www.f2er.com/xml/295907.html

猜你在找的XML相关文章