xstream 解析xml 别名 注解 list集合 boolean 数组 converter Pom.xml设置

<? xml version="1.0" encoding="UTF-8"  ?>
< person >
    
< name > yunyun </ name >
    
< gender > true </ gender >
    
< address > shanghai </ address >
    
< contents > cc </ contents >
    
< contents > ss </ contents >
    
< taxs >
        
< string > 1 </ string >
        
< string > 2 </ string >
        
< string > 3 </ string >
    
</ taxs >
</ person >

JavaBean
public   class  Person2  {
    
private String name;
    
private boolean gender;
    
private String homeAddress;
    
private List<String> contentslist;
    
private String[] taxs;

    @Override
    
public String toString() {
        
return ToStringBuilder.reflectionToString(this);
    }


}

测试类 解析xml 反序列化到javaBean object

public   class  TestXmlToObject2  {

     
public static final XStream xstream=new XStream();
     
     
static{
         xstream.alias(
"person", Person2.class);//类别名
         xstream.aliasField("address", Person2.class"homeAddress"); //属性别名
         xstream.alias("contents",String.class);//private List<String> contentslist;是string类型
         xstream.addImplicitCollection(Person2.class"contentslist");//隐式集合 隐藏contentslist
         Person2 person=(Person2)xstream.fromXML(TestXmlToObject2.class.getClassLoader().getResourceAsStream("person02.xml"));
         System.out.println(person.toString());
     }

    
    
public static void main(String[] args) {

    }


}


二: 注解方式
xml:
 
<? xml version="1.0" encoding="UTF-8"  ?>
< person >
    
< name > yunyun </ name >
    
< gender > true </ gender >
    
< address > shanghai </ address >
    
< company > yihaodian </ company >
    
< sources >
        
< string > firstSources </ string >
        
< string > secondSources </ string >
    
</ sources >
    
< part > firstPart </ part >
    
< part > secondPart </ part >
</ person >

javaBean
@XStreamAlias("person")
public class Person3 {
    private String name;
    private boolean gender;
    @XStreamAlias("address")
    private String homeAddress;
    private String company;
    @XStreamImplicit(itemFieldName="part")
    private List
< String >  content;
    private String[] sources;
    
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

测试
public   class  TestXmlToObject3  {

     
public static final XStream xstream=new XStream();
     
     
static{
         xstream.processAnnotations(Person3.
class);//显示声明使用注解
         xstream.autodetectAnnotations(true);
         Person3 person
=(Person3)xstream.fromXML(TestXmlToObject3.class.getClassLoader().getResourceAsStream("person03.xml"));
         System.out.println(person.toString());
     }

     
    
    
    
    
public static void main(String[] args) {

    }


}

POM.xml
         < dependency >
            
< groupId > com.thoughtworks.xstream </ groupId >
            
< artifactId > xstream </ artifactId >
            
< version > 1.3.1 </ version >
        
</ dependency >
        
< dependency >
            
< groupId > commons-lang </ groupId >
            
< artifactId > commons-lang </ artifactId >
            
< version > 2.4 </ version >
        
</ dependency >



你可能感兴趣的:(xstream 解析xml 别名 注解 list集合 boolean 数组 converter Pom.xml设置)