Search code examples
javajaxbxstream

Xstream: a list with XStreamImplicit's itemFieldName but still wrapped in an element


I have this class

@XStreamAlias("myConfig")
public class MyConfig {
    ... // other properties

    // What do I put here? 
    private List<String> includes;
}

And I want this output:

<myConfig>
    ...
    <includes>
        <includes>foo</include>
        <includes>bar</include>
    </includes>
</myConfig>

This doesn't work, because the element is lost:

    @XStreamImplicit(itemFieldName = "include") // Loses <includes> element
    private List<String> includes;

Solution

  • Since your questions is tagged with and based on your comment.

    @BlaiseDoughan Yes, jaxb too: I might consider switching from xstream to jaxb in the long run, because JAXB can generate an XSD.

    Here is what it would look like when using a JAXB (JSR-222) implementation:

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class MyConfig {
    
        private List<String> includes;
    
        @XmlElementWrapper
        @XmlElement(name = "include")
        public List<String> getIncludes() {
            return includes;
        }
    
        public void setIncludes(List<String> includes) {
            this.includes = includes;
        }
    
    }
    

    For More Information