Unmarshalling objects with same name for multiple elements

Sample xml file to unmarshalled

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

My java objects

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

Relationship between both types of 'category' is little confusing

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

I'm getting exception that Category can not be cast to ExtendedCategory Things work fine if I change top level xml element to something else (+ corresponding change in Extended category)

How to Unmarshalling objects with same name for multiple elements ?

Answers


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

I am trying

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

and I get category with 2 pathes and 10 subcategories, like you need. next:

JAXB.marshal(c, System.out);  

and I get same XML like in your example.

This class works well. For child categories set field path to null and field subCategories to null


Need Your Help

Mongo 3.0.5 - cannot create index on polygon

mongodb indexing geospatial

I created this document in a collection on Mongo 3.0.5

How to host a WPF control in an MS Access VBA Form?

wpf ms-access migration vsto legacy-code

We have a large MS Access project that requires GUI functionality that is difficult / impossible to do in the VBA / access forms environment.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.