views:

642

answers:

1

I have successfully marshaled the following class

@XmlRootElement(name = "Field")
private static class MyField {       
    @XmlAttribute(name = "Name")
    String name;
    @XmlElement(name = "Size")
    int size;
    ....}

Now I want to have my container class to hold multiple instances of Field, so I declared a class in the following way:

private static class MyFieldsCollection {     
    private Collection<MyField> fields = new LinkedList<MyField>();

    public MyFieldsCollection() {}
    ....}

When I simply try to marshal the container field I get the following error:

class java.util.LinkedList nor any of its super class is known to this context

How do I annotate the fields member so the container class will be marshaled as a collection of fields?

A: 

You shouldn't annotate your MyField class as a @XmlRootElement as it won't be a xml root element. I think you can either annotate it with @XmlType or simply do not annotate it. You could try and put the @XmlElement tag in the getter for the "fields" attribute in your class. If it works, your XML will look like this:

<MyFieldsCollection>
   <fields name="name1" size="size1"/>
   <fields name="name2" size="size2"/>
</MyFieldsCollection>

If it doesn't work, and it's a shot in the dark, try changing the type of the "Collection" to "List" to check if it makes some difference. What version of Java are you using? How does the code that you use to serialize your object looks like?

Ravi Wallau