我正在與JAXB元帥和unmarshal。我有以下情況,我必須使用相同的名稱(Person.ELEMENT_NAME)來處理2 @XmlRootElement
。JAXB中的多個類可以具有相同的元素名稱嗎?
@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());
}
}
在測試類中的字符串人類有以下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的整個人(John的描述)?
爲什麼我的第二個輸出中有兩個
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
?是否有可能通過使用
@XmlElement(name = Person.ELEMENT_NAME , type = ReceiverPerson.class)
或以任何其他方式在xml中有類似<person type="ReceiverPerson">
的內容?最後一段的輸出(解組所產生的XML,然後元帥)不提供正確的輸出,輸出的是這樣的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <human> <person> <name>Marry</name> <title>Lecturer</title> </person> <type>Human</type> </human>
出於某種原因,人子類是不正在生成。請任何想法嗎?
預先感謝任何人誰ATLEAST閱讀這個漫長的問題
嗨布萊斯,太感謝你了。我已經編輯了一下我的問題,請問你能看看嗎? – web2dev