JaxB解组问题
我尝试在以下线程中运行相同的示例: 但我收到以下例外情况: 意外元素(uri:,本地:“dog”)。预期元素为catchAll>JaxB解组问题,jaxb,Jaxb,我尝试在以下线程中运行相同的示例: 但我收到以下例外情况: 意外元素(uri:,本地:“dog”)。预期元素为catchAll> 知道为什么会出现这种异常吗?我设法运行了这个示例,但在java.uill.List中使用了XmlElements标记之后 代码如下: @XmlRootElement 班级动物园{ @XmlElements({ @XmlElement(name = "Dog" , type = Dog.class), @XmlElement(nam
知道为什么会出现这种异常吗?我设法运行了这个示例,但在java.uill.List中使用了XmlElements标记之后 代码如下:
@XmlRootElement
班级动物园{
@XmlElements({
@XmlElement(name = "Dog" , type = Dog.class),
@XmlElement(name = "Cat" , type = Cat.class)
})
private List<Animal> animals;
public static void main(String[] args) throws Exception {
Zoo zoo = new Zoo();
zoo.animals = new ArrayList<Animal>();
Dog doggy = new Dog();
doggy.setDogProp("Doggy");
Cat catty = new Cat();
catty.setCatProp("Catty");
zoo.animals.add(doggy);
zoo.animals.add(catty);
JAXBContext jc = JAXBContext.newInstance(Zoo.class, Dog.class, Cat.class);
Marshaller marshaller = jc.createMarshaller();
ByteArrayOutputStream os = new ByteArrayOutputStream();
marshaller.marshal(zoo, os);
System.out.println(os.toString());
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
Zoo unmarshalledZoo = (Zoo) unmarshaller.unmarshal(new ByteArrayInputStream(os.toByteArray()));
if (unmarshalledZoo.animals == null) {
System.out.println("animals was null");
} else if (unmarshalledZoo.animals.size() == 2) {
System.out.println("it worked");
} else {
System.out.println("failed!");
}
}
public interface Animal {
}
@XmlRootElement
public static class Dog implements Animal {
private String dogProp;
public String getDogProp() {
return dogProp;
}
public void setDogProp(String dogProp) {
this.dogProp = dogProp;
}
}
@XmlRootElement
public static class Cat implements Animal {
private String catProp;
public String getCatProp() {
return catProp;
}
public void setCatProp(String catProp) {
this.catProp = catProp;
}
}
@XmlElements({
@XmlElement(name=“Dog”,type=Dog.class),
@XmlElement(name=“Cat”,type=Cat.class)
})
私人动物名录;
公共静态void main(字符串[]args)引发异常{
动物园=新动物园();
zoo.anies=新ArrayList();
狗狗=新狗();
doggy.setDogProp(“doggy”);
猫猫=新猫();
catty.setCatProp(“catty”);
动物园。动物。添加(狗);
动物园。动物。加(斤);
JAXBContext jc=JAXBContext.newInstance(Zoo.class、Dog.class、Cat.class);
Marshaller=jc.createMarshaller();
ByteArrayOutputStream os=新建ByteArrayOutputStream();
马歇尔(动物园,os);
System.out.println(os.toString());
Unmarshaller Unmarshaller=jc.createUnmarshaller();
unmarshaller.setEventHandler(新的javax.xml.bind.helpers.DefaultValidationEventHandler());
Zoo unmarshalledZoo=(Zoo)unmarshaller.unmarshal(新的ByteArrayInputStream(os.toByteArray());
if(unmarshalledZoo.animals==null){
System.out.println(“动物为空”);
}else if(unmarshalledZoo.animals.size()==2){
System.out.println(“它成功了”);
}否则{
System.out.println(“失败!”);
}
}
公共界面动物{
}
@XmlRootElement
公共静态类狗具动物{
私人字符串道具;
公共字符串getDogProp(){
返回道具;
}
公共void setDogProp(字符串dogProp){
this.dogProp=dogProp;
}
}
@XmlRootElement
公共静态类Cat实现动物{
私有字符串catProp;
公共字符串getCatProp(){
返回catProp;
}
公共无效设置catProp(字符串catProp){
this.catProp=catProp;
}
}
}