我尝试在以下主题中运行相同的示例:
JAXB Annotations - Mapping interfaces and @XmlElementWrapper
但我收到以下例外:
意外元素(uri:“”,本地:“狗”)。预期元素是< {奇怪的问号符号}> catchAll>
...
知道我为什么会遇到这个例外吗?
答案 0 :(得分:1)
我设法运行了这个例子,但是在使用带有java.uill.List的XmlElements标记之后 这是代码:
@XmlRootElement
class Zoo {
@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)
})
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;
}
}