2013-12-14 106 views
1

我正在與JAXB元帥和unmarshal。我有以下情況,我必須使用相同的名稱(Person.ELEMENT_NAME)來處理2 @XmlRootElementJAXB中的多個類可以具有相同的元素名稱嗎?

@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> 

很抱歉的大實例,我的問題如下

  1. 爲什麼第一個輸出缺少這些人?

  2. 爲什麼第二個輸出缺少age

  3. 爲什麼第二個輸出會丟失ReceiverElement的整個人(John的描述)?

  4. 爲什麼我的第二個輸出中有兩個<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

  5. 是否有可能通過使用@XmlElement(name = Person.ELEMENT_NAME , type = ReceiverPerson.class)或以任何其他方式在xml中有類似<person type="ReceiverPerson">的內容?

  6. 最後一段的輸出(解組所產生的XML,然後元帥)不提供正確的輸出,輸出的是這樣的:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <human> 
    <person> 
        <name>Marry</name> 
        <title>Lecturer</title> 
    </person> 
    <type>Human</type> 
    </human> 
    

出於某種原因,人子類是不正在生成。請任何想法嗎?

預先感謝任何人誰ATLEAST閱讀這個漫長的問題

回答

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(約翰的說明)?

一旦你修復第1項,這將工作。

4 - 爲什麼我的第二個輸出中有兩個?

我沒有看到你得到這個聲明兩次。我運行演示代碼時看不到它兩次。

5 - 是否有可能通過使用@XmlElement(名稱= Person.ELEMENT_NAME,類型= ReceiverPerson.class),或以任何其他方式 有在XML的東西怎麼樣?

由於您的屬性是在子類級別鍵入的,因此不需要任何類型的限定符。當需要類型限定符時,JAXB將利用xsi:type屬性。您可能會發現以下有趣:

+0

嗨布萊斯,太感謝你了。我已經編輯了一下我的問題,請問你能看看嗎? – web2dev

相關問題