posts - 110, comments - 101, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
<?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>




评论

# re: xstream 解析xml 别名 注解 list集合 boolean 数组 converter Pom.xml设置  回复  更多评论   

2013-03-27 17:30 by hello_kitty
<list>
<string>CALCULATE</string>
<string>caihongcheng</string>
<int>10</int>
<boolean-array>
<boolean>true</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</boolean-array>
</list>
一开始有一个boolean类型的数组,我把它添加到一个list里面,想通过xstream遍历成xml。(源代码:
ArrayList list = new ArrayList();
list.add("CALCULATE");
list.add("caihongcheng");
list.add(10);
list.add(status);//status为boolean数组


求大神给写段代码让上面的数组变成下面那样的,可以把代码发给我邮箱:513110286@qq.com,不胜感谢!

<response>
<command>CALCULATE</command>
<parkName>caihongcheng</parkName>
<picNumber>20</picNumber>
<picValue>true</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
<picValue>false</picValue>
</response>

# re: xstream 解析xml 别名 注解 list集合 boolean 数组 converter Pom.xml设置[未登录]  回复  更多评论   

2014-12-29 14:18 by xiaoxiao
谢谢分享!帮我解决了隐式集合的问题!

# re: xstream 解析xml 别名 注解 list集合 boolean 数组 converter Pom.xml设置  回复  更多评论   

2016-03-26 17:06 by lzk
非常感谢!

只有注册用户登录后才能发表评论。


网站导航: