2012-09-08 60 views
1

示例XML文件解組與多個元素同名解組對象

<category> 
    <id>abcat0010000</id> 
    <name>Gift Center</name> 
    <path> 
     <category> 
     <id>cat00000</id> 
     <name>Best Buy</name> 
     </category> 
     <category> 
     <id>abcat0010000</id> 
     <name>Gift Center</name> 
     </category> 
    </path> 
    <subCategories> 
     <category> 
     <id>pcmcat140000050035</id> 
     <name>Capturing Photos &amp; Videos</name> 
     </category> 
     <category> 
     <id>pcmcat140000050036</id> 
     <name>Listening to Digital Music</name> 
     </category> 
     <category> 
     <id>pcmcat140000050037</id> 
     <name>Computing Made Easy</name> 
     </category> 
     <category> 
     <id>pcmcat140000050039</id> 
     <name>Simple GPS Navigation</name> 
     </category> 
     <category> 
     <id>pcmcat140000050040</id> 
     <name>Playing Video Games</name> 
     </category> 
     <category> 
     <id>pcmcat140000050041</id> 
     <name>Watching HDTV</name> 
     </category> 
     <category> 
     <id>pcmcat140000050042</id> 
     <name>Enjoying Favorite Movies</name> 
     </category> 
     <category> 
     <id>abcat0012000</id> 
     <name>Him</name> 
     </category> 
     <category> 
     <id>abcat0013000</id> 
     <name>Teens</name> 
     </category> 
     <category> 
     <id>abcat0014000</id> 
     <name>Kids</name> 
     </category> 
    </subCategories> 
    </category> 

我的Java對象這兩種類型的 '類' 之間

@XmlRootElement(name="category") 
    public class Category { 
     String id; 
     String name; 

     public String getId() { 
      return id; 
     } 

     @XmlElement 
     public void setId(String id) { 
      this.id = id; 
     } 

     public String getName() { 
      return name; 
     } 

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

關係是有點混亂

@XmlRootElement("category") 
public class ExtendedCategory extends Category { 
    /*String id; 
    String name;*/ 
    List<Category> path; 
    List<Category> subCategories; 

    /*public String getId() { 
     return id; 
    } 

    @XmlElement 
    public void setId(String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    @XmlElement 
    public void setName(String name) { 
     this.name = name; 
    } 
*/ 
    public List<Category> getPath() { 
     return path; 
    } 

    @XmlElementWrapper(name="path") 
    @XmlElement(name="category", type=Category.class) 
    public void setPath(List<Category> path) { 
     this.path = path; 
    } 

    public List<Category> getSubCategories() { 
     return subCategories; 
    } 

    @XmlElementWrapper(name="subCategories") 
    @XmlElement(name="category", type=Category.class) 
    public void setSubCategories(List<Category> subCategories) { 
     this.subCategories = subCategories; 
    } 
} 

我收到異常,無法將類別投射到ExtendedCategory 如果我將頂級xml元素更改爲其他內容(+擴展類別中的+相應更改),則事情正常工作

如何爲多個元素解組具有相同名稱的對象?

回答

1
@XmlRootElement(name = "category") 
public class Category { 

    String id; 
    String name; 
    List<Category> path; 
    List<Category> subCategories; 

    public String getId() { 
     return id; 
    } 

    @XmlElement 
    public void setId(String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public List<Category> getPath() { 
     return path; 
    } 

    @XmlElementWrapper(name = "path") 
    @XmlElement(name = "category", type = Category.class) 
    public void setPath(List<Category> path) { 
     this.path = path; 
    } 

    public List<Category> getSubCategories() { 
     return subCategories; 
    } 

    @XmlElementWrapper(name = "subCategories") 
    @XmlElement(name = "category", type = Category.class) 
    public void setSubCategories(List<Category> subCategories) { 
     this.subCategories = subCategories; 
    } 
} 

我想

Category c = JAXB.unmarshal(new File("yourXml"), Category.class); 

,我得到類別2個pathes和10個小類,像你所需要的。
未來:

JAXB.marshal(c, System.out); 

,我也得到相同的XML就像在你的榜樣。

該課程運作良好。對於子類別設置字段pathnull和字段subCategoriesnull

+0

顯然,我從來沒有嘗試過像你這樣簡單的類。 #FML – user401445