Java JAXB 解组 @XmlAnyElement
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20329510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB Unmarshalling @XmlAnyElement
提问by hiddenuser
I have created three JAXB class : Home , Person , Animal
. Java Class
Home have variable List<Object> any
that may contain Person and/or Animal instance .
我已经创建了三个JAXB类:Home , Person , Animal
。Java Class Home 具有List<Object> any
可能包含 Person 和/或 Animal 实例的变量。
public class Home {
@XmlAnyElement(lax = true)
protected List<Object> any;
//setter getter also implemented
}
@XmlRootElement(name = "Person") // Edited
public class Person {
protected String name; //setter getter also implemented
}
@XmlRootElement(name = "Animal") // Edited
public class Animal {
protected String name; //setter getter also implemented
}
/* After Unmarshalling*/
/*解组后*/
Home home ;
for(Object obj : home .getAny()){
if(obj instanceof Person ){
Person person = (Person )obj;
// .........
}else if(obj instanceof Animal ){
Animal animal = (Animal )obj;
// .........
}
}
I need to achieve Person or Animal
object saved in "Home.any" List
variable but content of "Home.any" List
is instance of com.sun.org.apache.xerces.internal.dom.ElementNSImpl
instead of Animal or Person
.
我需要实现Person or Animal
保存在"Home.any" List
变量中的对象,但内容"Home.any" List
是实例com.sun.org.apache.xerces.internal.dom.ElementNSImpl
而不是Animal or Person
.
So is there a way to achieve Animal or Person
instance that is saved in xml in "Home.any" List
.
那么有没有办法实现Animal or Person
保存在 .xml 中的实例"Home.any" List
?
采纳答案by bdoughan
You need to add @XmlRootElement
on the classes you want to appear as instances in the field/property you have annotated with @XmlAnyElement(lax=true)
.
您需要添加@XmlRootElement
要在已使用 注释的字段/属性中显示为实例的类@XmlAnyElement(lax=true)
。
Java Model
Java模型
Home
家
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Home {
@XmlAnyElement(lax = true)
protected List<Object> any;
//setter getter also implemented
}
Person
人
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Person")
public class Person {
}
}
Animal
动物
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Animal")
public class Animal {
}
Demo Code
演示代码
input.xml
输入文件
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Person/>
<Animal/>
<Person/>
</root>
Demo
演示
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(Home.class, Person.class, Animal.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/forum20329510/input.xml");
Home home = unmarshaller.unmarshal(xml, Home.class).getValue();
for(Object object : home.any) {
System.out.println(object.getClass());
}
}
}
Output
输出
class forum20329510.Person
class forum20329510.Animal
class forum20329510.Person