I am new to XStream
I have following DTO
@XStreamAlias("outline")
public class OutlineItem implements java.io.Serializable {
private static final long serialVersionUID = -2321669186524783800L;
@XStreamAlias("text")
@XStreamAsAttribute
private String text;
@XStreamAlias("removeMe")
private List<OutlineItem> childItems;
}
once i do
XStream stream = new XStream();
stream.processAnnotations(OutlineItem.class);
stream.toXML(outlineItem);
i get this as my output text
<outline text="Test">
<removeMe>
<outline text="Test Section1">
<removeMe>
<outline text="Sub Section1 1">
<removeMe/>
</outline>
<outline text="Sub Section1 2">
<removeMe/>
</outline>
</removeMe>
</outline>
<outline text="Test Section 2">
<removeMe>
<outline text="Test Section2 1">
<removeMe/>
</outline>
</removeMe>
</outline>
</removeMe>
</outline>
whereas i want the output to be:
<outline text="Test">
<outline text="Test Section1">
<outline text="Sub Section1 1">
</outline>
<outline text="Sub Section1 2">
</outline>
</outline>
<outline text="Test Section 2">
<outline text="Test Section2 1">
</outline>
</outline>
</outline>
Any help will be greatly appreciated! Not sure if some kind of XSLT is required...
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.
I believe the answer is:
@XStreamImplicit(itemFieldName="outline")
private List<OutlineItem> childItems;
Have you considered using a JAXB implementation (Metro, MOXy, JaxMe, ...) instead?
OutlineItem
import javax.xml.bind.annotation.*;
@XmlRootElement(name="outline")
@XmlAccessorType(XmlAccessType.FIELD)
public class OutlineItem implements java.io.Serializable {
private static final long serialVersionUID = -2321669186524783800L;
@XmlAttribute
private String text;
@XmlElement("outline")
private List<OutlineItem> childItems;
}
Demo
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(OutlineItem.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(outlineItem, System.out);
}
}