使用XStream需注意的问题

         这些问题是我在实际工作中遇到的,遇到一个问题就记一个问题,因为我这人脑子不太好,过段时间就会想不起来怎么解决了。

(一) 类的继承关系,在转化为xml时,父类节点应添加class属性,指明当前是哪个子类继承了它,否则在由xml转化为Object时会出错,因为XStream并不知道该把当前节点实例化为哪种类型。 

(1)一个抽象类 Animal.java

package  xml_chb;

public   abstract   class  Animal  {
    
private int age;

    
public int getAge() {
        
return age;
    }


    
public void setAge(int age) {
        
this.age = age;
    }

}

(2) Pig继承自Animal

 

package  xml_chb;

public   class  Pig  extends  Animal {
    
private int weight;

    
public int getWeight() {
        
return weight;
    }


    
public void setWeight(int weight) {
        
this.weight = weight;
    }

}

(3) AnimalContor类中包含Animal类型的变量

package  xml_chb;

public   class  AnimalContor  {
    
private String id;
    
private Animal animal;
    
public Animal getAnimal() {
        
return animal;
    }

    
public void setAnimal(Animal animal) {
        
this.animal = animal;
    }

    
public String getId() {
        
return id;
    }

    
public void setId(String id) {
        
this.id = id;
    }

    
}

(4) 将AnimalContor对象转化为xml

         // 实例化一个AnimalContor对象
        AnimalContor ac = new  AnimalContor();
        ac.setId(
" 0001 " );
        
        
// 生成Pig对象,并作为AnimalContor的Animal变量实例
        Pig pig = new  Pig();
        pig.setAge(
10 );
        pig.setWeight(
100 );
        ac.setAnimal(pig);
        
        
// 将对象转化为xml
        XStream xstream = new  XStream( new  DomDriver());
        xstream.alias(
" animalcontor " ,AnimalContor. class );
        String strxml
= xstream.toXML(ac);
        System.out.println(strxml);

输出:

< animalcontor >
  
< id > 0001 </ id >
  
< animal  class ="xml_chb.Pig" >
    
< weight > 100 </ weight >
    
< age > 10 </ age >
  
</ animal >
</ animalcontor >

注意,animal节点有class属性,表明当前类由哪个子类来实现

(5) 将xml转化为AnimalContor对象

animal节点有class属性

String str1 = " <animalcontor><id>0002</id><animal class='xml_chb.Pig'><weight>100</weight><age>11</age></animal></animalcontor> " ;
        AnimalContor ac1
= (AnimalContor)xstream.fromXML(str1);
        System.out.println(ac1.getId());

输出:0002

animal节点没有class属性,会抛出异常

String str2 = " <animalcontor><id>0003</id><animal><weight>100</weight><age>11</age></animal></animalcontor> " ;
        AnimalContor ac2
= (AnimalContor)xstream.fromXML(str2);
        System.out.println(ac2.getId());

抛出如下异常:

java.lang.InstantiationError: xml_chb.Animal
 at sun.reflect.GeneratedSerializationConstructorAccessor3.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
 at com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider.newInstance(Sun14ReflectionProvider.java:54)
 at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(AbstractReflectionConverter.java:223)
 at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:117)
 at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
 at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
 at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
 at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:182)
 at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:159)
 at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:118)
 at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
 at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
 at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
 at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:117)
 at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.unmarshal(ReferenceByXPathMarshallingStrategy.java:29)
 at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:826)
 at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:813)
 at com.thoughtworks.xstream.XStream.fromXML(XStream.java:761)
 at com.thoughtworks.xstream.XStream.fromXML(XStream.java:753)
 at xml_chb.AnimalContor.main(AnimalContor.java:41)
Exception in thread "main"

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值