为多个元素取消对具有相同名称的对象的编组

  • 本文关键字:对象 元素 取消 java xml jaxb
  • 更新时间 :
  • 英文 :


要解组的示例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;
    }
}

我得到了一个异常,即Category不能强制转换为ExtendedCategory如果我将顶级xml元素更改为其他元素(+扩展类别中的相应更改),事情会很好

如何为多个元素解组具有相同名称的对象?

@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个路径和10个子类别的类别,就像你需要的那样
下一篇:

JAXB.marshal(c, System.out);  

我得到了和您的例子中相同的XML。

这门课效果很好。对于子类别,设置字段pathnull,字段subCategoriesnull

最新更新