我正在使用JAXB元帅和解组。我有以下场景,我必须为2 @XmlRootElement
使用相同的名称(Person.ELEMENT_NAME)。
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Person{
public final static String ELEMENT_NAME = "person";
@XmlElement
private String name;
@XmlElement
private String title;
//Getter Setter
}
@XmlRootElement(name = Person.ELEMENT_NAME)
public class ReceiverPerson extends Person {
@XmlElement
private String gender;
//Getter Setter
}
@XmlRootElement(name = Person.ELEMENT_NAME)
public class SenderPerson extends Person {
@XmlElement
private String age;
//Getter Setter
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Human {
@XmlElement(name = Person.ELEMENT_NAME , type = ReceiverPerson.class)
private ReceiverPerson rP;
@XmlElement(name = Person.ELEMENT_NAME , type= SenderPerson.class)
private SenderPerson sP;
@XmlElement
private String type;
@XmlAnyElement
private List<Object> unknown;
//Getter Setter
}
public class Test{
public static void main(String[] args){
StringWriter sw = new StringWriter();
JAXBContext jc = JAXBContext.newInstance(Human.class);
Marshaller marshaller = jc.createMarshaller();
Unmarshaller unmarshaller = jc.createUnmarshaller();
Human human2,human1;
String humans = "This xml is writtem after my code , because its too big";
human1 = new Human();
human1.setType("Human");
SenderPerson sp = new SenderPerson();
sp.setAge("23");
sp.setName("Marry");
sp.setTitle("Lecturer");
ReceiverPerson rP = new ReceiverPerson();
rP.setName("John");
rP.setTitle("Professor");
rP.setGender("Male");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(human1, sw);
System.out.println("Marshalling by creating a Human object");
System.out.println(sw.toString());
human2 = (Human) unmarshaller.unmarshal(new StringReader(sw.toString()));
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(human2, sw);
System.out.println("Marshalling from my own xml");
System.out.println(sw.toString());
}
}
Test类中的String人员遵循xml:
<human>
<type>Human Being</type>
<person type="ReceiverPerson">
<name>John</name>
<title>Professor</title>
<gender>Male</gender>
</person>
<person type="SenderPerson">
<name>Marry</name>
<title>Lecturer</title>
<age>23</age>
</person>
<anything>I am unknown to the xml</anything>
</human>
我不知道发生了什么,但我得到了以下输出:
Marshalling by creating a Human object
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
<type>Human</type>
</human>
Marshalling from my own xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
<type>Human</type>
</human>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
<person>
<name>Marry</name>
<title>Lecturer</title>
</person>
<type>Human Being</type>
<anything>I am unknown to the xml</anything>
</human>
对不起,我的问题如下
为什么第一个输出缺少人?
为什么第二个输出缺少age
?
为什么第二个输出缺少ReceiverElement的整个人(约翰的描述)?
为什么我的第二个输出中有两个<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
?
是否可以使用<person type="ReceiverPerson">
或以任何其他方式在xml中使用@XmlElement(name = Person.ELEMENT_NAME , type = ReceiverPerson.class)
这样的内容?
最后一段的输出(解组生成的xml然后编组)没有给出正确的输出,输出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
<person>
<name>Marry</name>
<title>Lecturer</title>
</person>
<type>Human</type>
</human>
由于某种原因,没有生成Person子类。好吗?
提前感谢任何至少读过这个冗长问题的人
答案 0 :(得分:1)
1 - 为什么第一个输出缺少人?
您缺少以下来电:
human1.setsP(sp);
human1.setrP(rP);
2 - 为什么第二个输出缺少年龄?
修复第1项后,您会在第二项输出中获得age
:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
<person>
<name>John</name>
<title>Professor</title>
<gender>Male</gender>
</person>
<person>
<name>Marry</name>
<title>Lecturer</title>
<age>23</age>
</person>
<type>Human</type>
</human>
3 - 为什么第二个输出错过了整个人 ReceiverElement(John的描述)?
修复第1项后,这将有效。
4 - 为什么我的第二个输出中有两个?
我没有看到你两次获得此声明。我运行你的演示代码时没有看到它两次。
5 - 是否可以使用@XmlElement(name =。)在xml中使用类似的东西 Person.ELEMENT_NAME,type = ReceiverPerson.class)或以任何其他方式 ?
由于属性是在子类级别键入的,因此不需要任何类型的限定符。当需要类型限定符时,JAXB将利用xsi:type
属性。您可能会发现以下有趣的内容: