这些问题是我在实际工作中遇到的,遇到一个问题就记一个问题,因为我这人脑子不太好,过段时间就会想不起来怎么解决了。
(一) 类的继承关系,在转化为xml时,父类节点应添加class属性,指明当前是哪个子类继承了它,否则在由xml转化为Object时会出错,因为XStream并不知道该把当前节点实例化为哪种类型。
(1)一个抽象类 Animal.java
packagexml_chb;

publicabstractclassAnimal...{
privateintage;

publicintgetAge()...{
returnage;
}

publicvoidsetAge(intage)...{
this.age=age;
}
}
(2) Pig继承自Animal
packagexml_chb;

publicclassPigextendsAnimal...{
privateintweight;

publicintgetWeight()...{
returnweight;
}

publicvoidsetWeight(intweight)...{
this.weight=weight;
}
}
(3) AnimalContor类中包含Animal类型的变量
packagexml_chb;

publicclassAnimalContor...{
privateStringid;
privateAnimalanimal;
publicAnimalgetAnimal()...{
returnanimal;
}
publicvoidsetAnimal(Animalanimal)...{
this.animal=animal;
}
publicStringgetId()...{
returnid;
}
publicvoidsetId(Stringid)...{
this.id=id;
}
}
(4) 将AnimalContor对象转化为xml
//实例化一个AnimalContor对象
AnimalContorac=newAnimalContor();
ac.setId("0001");
//生成Pig对象,并作为AnimalContor的Animal变量实例
Pigpig=newPig();
pig.setAge(10);
pig.setWeight(100);
ac.setAnimal(pig);
//将对象转化为xml
XStreamxstream=newXStream(newDomDriver());
xstream.alias("animalcontor",AnimalContor.class);
Stringstrxml=xstream.toXML(ac);
System.out.println(strxml);
输出:
<animalcontor>
<id>0001</id>
<animalclass="xml_chb.Pig">
<weight>100</weight>
<age>10</age>
</animal>
</animalcontor>
注意,animal节点有class属性,表明当前类由哪个子类来实现
(5) 将xml转化为AnimalContor对象
animal节点有class属性
Stringstr1="<animalcontor><id>0002</id><animalclass='xml_chb.Pig'><weight>100</weight><age>11</age></animal></animalcontor>";
AnimalContorac1=(AnimalContor)xstream.fromXML(str1);
System.out.println(ac1.getId());
输出:0002
animal节点没有class属性,会抛出异常
Stringstr2="<animalcontor><id>0003</id><animal><weight>100</weight><age>11</age></animal></animalcontor>";
AnimalContorac2=(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"
本文介绍使用XStream进行Java对象与XML之间的转换时,如何通过添加class属性解决类继承关系问题,确保子类对象能正确转换。
1525

被折叠的 条评论
为什么被折叠?



