2012-05-10 138 views
2

我有一些對象讓我們說兩個,A和B.這些對象來自同一個類。我需要使用JAXB編組這些對象和XML輸出應該是這種形式:需要幫助格式化JAXB輸出

<Root> 
    <A> 
     <ID> an id </ID> 
    </A> 
    <B> 
     <ID> an id </ID> 
    </B> 
</Root> 

<!-- Then all A and B attributes must be listed !--> 
<A> 
    <ID> an id </ID> 
    <attribute1> value </attribute1> 
    <attribute2> value </attribute2> 
</A> 
<B> 
    <ID> an id </ID> 
    <attribute1> value </attribute1> 
    <attribute2> value </attribute2> 
</B> 

如何生成JAXB這種格式?任何幫助表示讚賞。

更新: 更具體地講,假設我們有世界級的是這樣的:

@XmlRootElement 
public class Human { 
    private String name; 
    private int age; 
    private Integer nationalID; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public Integer getNationalID() { 
     return nationalID; 
    } 

    public void setNationalID(Integer nationalID) { 
     this.nationalID = nationalID; 
    } 
} 

和我們的主類是:

public class Main3 { 

    public static void main(String[] args) throws JAXBException { 
     Human human1 = new Human(); 
     human1.setName("John"); 
     human1.setAge(24); 
     human1.setNationalID(Integer.valueOf(123456789)); 

     JAXBContext context = JAXBContext.newInstance(Human.class); 
     Marshaller m = context.createMarshaller(); 
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 

     StringWriter stringWriter = new StringWriter(); 

     m.marshal(human1, stringWriter); 

     System.out.println(stringWriter.toString()); 
    } 

} 

然後輸出將是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<human> 
    <age>24</age> 
    <name>John</name> 
    <nationalID>123456789</nationalID> 
</human> 

現在我需要的輸出是像這樣:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<human> 
    <nationalID>123456789</nationalID> 
</human> 
<human> 
    <nationalID>123456789</nationalID> 
    <age>24</age> 
    <name>John</name> 
</human> 

,這將有助於我畫XML對象的樹沒有屬性(僅ID),然後,但樹下面的所有定義。這可能使用JAXB或任何其他實現?

回答

8

試試這個:

import java.io.StringWriter; 
import javax.xml.bind.Marshaller; 

... 

Object requestObject = ... // This is the object that needs to be printed with indentation 
Marshaller marshaller = ... 
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
StringWriter stringWriter = new StringWriter(); 
marshaller.marshal(requestObject, stringWriter); 

System.out.println(stringWriter.toString()); 
+0

不幸的是,這不是我所需要的每個節點都包含它的屬性,這將產生一個XML。我更清楚地向我提出了這個問題(我的壞,對不起)。 –

+0

我不認爲有一個解決方案,你想用jaxb做什麼,因爲它對命名過於嚴格。你打算如何使用xml,也許你可以用其他解析器生成它,或者在最壞的情況下,你可以創建自己的解析器來完成這個任務。我試圖創建你所期望的,但它只能用額外的標籤和/或命名空間。 – pXel

+0

非常感謝您的回覆。你能告訴我你使用了多少額外的標籤嗎?我可以使用任何我需要的標籤。另外你認爲還有哪些解析器可以幫助我解決這個問題?該解決方案不必嚴格命名,但它應該只使用ID創建一個對象樹,對象的屬性應該在樹下,這是唯一重要的事情。先生, –

3
package com.namasoft.dms.gui.common.utilities; 

import java.io.StringWriter; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.annotation.XmlAccessorOrder; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementWrapper; 
import javax.xml.bind.annotation.XmlID; 
import javax.xml.bind.annotation.XmlIDREF; 
import javax.xml.bind.annotation.XmlRootElement; 

public class JAXB 
{ 

    public static class Human 
    { 

     @XmlID 
     String id; 
     @XmlElement 
     String name; 
     @XmlElement 
     int age; 

     public Human() 
     { 
     } 

     public Human(String name) 
     { 
      this.id = this.name = name; 
      age = new Random().nextInt(); 
     } 
    } 

    @XmlRootElement 
    public static class HumansList 
    { 
     @XmlElementWrapper(name = "humanObjects") 
     @XmlElement(name = "human") 
     List<Human> humanObjects = new ArrayList<>(); 

     @XmlIDREF 
     @XmlElementWrapper(name = "humanIds") 
     @XmlElement(name = "id") 
     List<Human> humanIds = new ArrayList<>(); 

     void addHuman(Human human) 
     { 
      humanObjects.add(human); 
      humanIds.add(human); 
     } 
    } 

    public static void main(String[] args) throws JAXBException 
    { 
     HumansList list = new HumansList(); 
     Human parent1 = new Human("parent1"); 
     list.addHuman(parent1); 
     Human child11 = new Human("child11"); 
     list.addHuman(child11); 
     Human child12 = new Human("child12"); 
     list.addHuman(child12); 

     Human parent2 = new Human("parent2"); 
     list.addHuman(parent2); 
     Human child21 = new Human("child21"); 
     list.addHuman(child21); 
     Human child22 = new Human("child22"); 
     list.addHuman(child22); 

     JAXBContext context = JAXBContext.newInstance(HumansList.class); 
     Marshaller m = context.createMarshaller(); 
     StringWriter xml = new StringWriter(); 
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
     m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 

     m.marshal(list, xml); 
     System.out.println(xml); 
    } 
} 

輸出將是

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<humansList> 
    <humanObjects> 
     <human> 
      <id>parent1</id> 
      <name>parent1</name> 
      <age>-486071665</age> 
     </human> 
     <human> 
      <id>child11</id> 
      <name>child11</name> 
      <age>920318383</age> 
     </human> 
     <human> 
      <id>child12</id> 
      <name>child12</name> 
      <age>-1355111983</age> 
     </human> 
     <human> 
      <id>parent2</id> 
      <name>parent2</name> 
      <age>-314154051</age> 
     </human> 
     <human> 
      <id>child21</id> 
      <name>child21</name> 
      <age>983544381</age> 
     </human> 
     <human> 
      <id>child22</id> 
      <name>child22</name> 
      <age>748252616</age> 
     </human> 
    </humanObjects> 
    <humanIds> 
     <id>parent1</id> 
     <id>child11</id> 
     <id>child12</id> 
     <id>parent2</id> 
     <id>child21</id> 
     <id>child22</id> 
    </humanIds> 
</humansList> 
+0

道具給你。 –