Search code examples
javaxmljaxbdefaultlistmodel

How to create a list from a DefaultModelList so I can marshal to XML using JAXB?


I am very new to this, so please be patient if I don't provide the right information.

I am trying to marshal data to xml using JAXB. The data is in a DefaultListModel. I am trying to move this data into a List that JAXB will recognize. This attempt gives me an StackOverflowError:

    @XmlElement
    Window R = new Window ();
    {
        List r = new ArrayList(); 
        for( int index=0;index<8;index++ ) {
            try {
                r.add(order.elementAt(index));
            } finally {
            }
        }
    }

The DefaultListModel is "order" Can I fix this or am I way off base on how to do this? Any suggestions would be really appreciated.


Solution

  • This is a class that has the list you want to marshal as a property:

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PROPERTY)
    public class Container {
        private List<Item> order;
        @XmlElement
        public List<Item> getOrder(){
            if( order == null ){
                order = new ArrayList<>();
            }
            return order;
        }
    }
    

    You may have to annotate Item (or whatever the class name of your DefaultListModel's element).

    The setter for order is typically omitted; one uses

    container.getOrder().add( anotherItem );
    

    to build the list.