Remove tag in XML generation with Xstream

Asked

Viewed 145 times

2

How can we remove the tag <list> automatically generated on XStream during the serialization process? I have three classes for this scenario: one for test and two to feed data that will be generated in xml.

Test class:

public static void main(String[] args) {

ArrayList<Object> balancos = new ArrayList<Object>();
XStreamBalancoTester tester = new XStreamBalancoTester();

//Serialização
XStream xstream = new XStream(new DomDriver()); //Stax imprime em linha unica com cabeçalho
xstream.autodetectAnnotations(true);
xstream.alias("balanco", Balanco.class);
xstream.alias("operacao", Operacao.class);      
xstream.setMode(XStream.NO_REFERENCES);  
balancos = tester.getBalancos(5);
String xml = xstream.toXML(balancos);
System.out.println(xml);

XML Generated:

<list>
<balanco>
<id >5< /id>
<compras>
<operacao>
<papel>ub22< /papel>
<valor>30.62< /valor>
<quantidade>150.0< /quantidade>
<data>2020-01-27 19:50:12.937 UTC< /data>
</operacao>
</compras>
</balanco>
</list>

I’ve tried with xstream.omitField(Name.class, “tag”) and xstream.addImplicitCollection(Name.class, “tag”) but they didn’t work out.

1 answer

0


In accordance with this post, you can use the Annotation @XStreamImplicit as follows:

   @XStreamImplicit(itemFieldName="OrderLine")
   ArrayList<OrderLine> orderLines;

Or using the method addImplicitCollection as below:

xstream.addImplicitCollection(Order.class, "orderLines", "OrderLine", OrderLine.class);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.