1

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;
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • Since your question is tagged with `jaxb` are you interested in the JAXB (JSR-222) solution? – bdoughan Jan 25 '13 at 11:05
  • 1
    @BlaiseDoughan Yes, jaxb too: I might consider switching from xstream to jaxb in the long run, because JAXB can generate an XSD. – Geoffrey De Smet Jan 25 '13 at 12:17
  • 1
    Great, I have added an answer demonstrating how this can be done with a JAXB (JSR-222) implementation: http://stackoverflow.com/a/14521810/383861 – bdoughan Jan 25 '13 at 12:28

2 Answers2

2

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

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 2
    Great stuff, especially the xstream vs jaxb comparison is beautiful :) Jaxb is looking more and more appealing. Out of scope for this question: My only "fear" with JAXB is that many app servers already have a jaxb version and I need my app to work on old app servers while using a just-released jaxb version. Is there any base to that fear? – Geoffrey De Smet Jan 28 '13 at 12:59
  • 1
    @GeoffreyDeSmet - Some app servers provide mechanisms to override the version of the implementation of JAXB that is used. Alternatively since JAXB is a standard (JSR-222) you could use a different provider (I'm the EclipseLink JAXB (MOXy) lead). Is there a particular extension you need from the latest version? Don't estimate the battle tested version already included. – bdoughan Jan 28 '13 at 13:14
  • BlaiseDoughan - Probably just uncertainty from my side indeed. Tnx for the info :) The only stuff I worry about so far is this @XmlElementWrapper, [the jaxb stuff I needed here](http://stackoverflow.com/questions/11484532/polymorphism-in-xstream-serialization-and-deserialization) and XSD generation. In any case, I 'll probably add an arquillian test to see if it works on old app servers (JBoss 5, Jetty 6 and tomcat 6). – Geoffrey De Smet Jan 28 '13 at 14:26
  • 1
    @BlaiseDoughan Excellent links ! – Zenil Feb 01 '13 at 01:09
1

To get the exact xml structure you are looking for, slight tweak needs to be done to your code. Basically you need a Includes wrapper object that will have the list of values.

Here's my code that generates the xml

public class XSTreamAnnotTest {

    static class Includes {

        @XStreamImplicit(itemFieldName = "include")
        private List<String> include;

        Includes(List<String> include) {
            this.include = include;
        }
    }

    @XStreamAlias("myConfig")
    static class MyConfig {

        private Includes includes;

        public MyConfig(Includes includes) {
            this.includes = includes;
        }

    }

    public static void main(String args[]) throws Exception {
        XStream stream = new XStream();
        stream.processAnnotations(MyConfig.class);
        ArrayList<String> list = new ArrayList<String>();
        list.add("foo");
        list.add("bar");
        Includes cu = new Includes(list);

        MyConfig msg = new MyConfig(cu);
        System.out.println(stream.toXML(msg));
    }
}
Zenil
  • 1,491
  • 3
  • 12
  • 21