这些问题是我在实际工作中遇到的,遇到一个问题就记一个问题,因为我这人脑子不太好,过段时间就会想不起来怎么解决了。
(一) 类的继承关系,在转化为xml时,父类节点应添加class属性,指明当前是哪个子类继承了它,否则在由xml转化为Object时会出错,因为XStream并不知道该把当前节点实例化为哪种类型。
(1)一个抽象类 Animal.java
public abstract class Animal ... {
private int age;
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
}
(2) Pig继承自Animal
public class Pig extends Animal ... {
private int weight;
public int getWeight() ...{
return weight;
}
public void setWeight(int weight) ...{
this.weight = weight;
}
}
(3) AnimalContor类中包含Animal类型的变量
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 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);
输出:
< id > 0001 </ id >
< animal class ="xml_chb.Pig" >
< weight > 100 </ weight >
< age > 10 </ age >
</ animal >
</ animalcontor >
注意,animal节点有class属性,表明当前类由哪个子类来实现
(5) 将xml转化为AnimalContor对象
animal节点有class属性
AnimalContor ac1 = (AnimalContor)xstream.fromXML(str1);
System.out.println(ac1.getId());
输出:0002
animal节点没有class属性,会抛出异常
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"