2012-09-08 6 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> 

내 자바가 '카테고리'의 두 가지 유형의 사이

@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; 
    } 
} 
입니다

Category가 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); 

을 시도하고 당신이 필요로하는 것처럼,이 명하는 Pathes 10 개 하위 범주와 범주를 얻을.
다음 :

JAXB.marshal(c, System.out); 

와 나는 당신의 예에서와 같은 동일한 XML을 얻을.

이 클래스는 잘 작동합니다. 하위 카테고리의 경우 pathnull으로, 필드를 subCategories부터 null까지

+0

분명히 나는 ​​당신과 같은 간단한 수업을 시도하지 않았습니다. #FML – user401445

관련 문제