I use Spring batch in order to parse XML documents and act upon the data contained in the those XML documents.
I have documents such as the one below:
<rootDoc>
<group id="13001L000001" box="13000B000001" operator="jsmith">
<mail numMail="3A06049714560"/>
<mail numMail="3A06049714561"/>
<mail numMail="3A06049714562"/>
<mail numMail="3A06049714565"/>
</group>
<group id="13001L000002" box="13000B000001" operator="jsmith">
<mail numMail="3A06049714570"/>
<mail numMail="3A06049714571"/>
<mail numMail="3A06049714572"/>
<mail numMail="3A06049714575"/>
</group>
</rootDoc>
Ideally, for each document I want to be able to obtain a collection of group
s and for each group a collection of mail
s
I am not sure how to use XStream for that purpose... Is it possible to achieve what I want to do with XStream or do I need JaxB?
XStream would be the 'easiest' imho - essentially allowing you to quickly construct a set of POJO's that would map the document and then use XStream configurations to parse the document into them. for instance for the rootDoc;
import java.io.Serializable;
import java.util.List;
public class Root implements Serializable {
private List<Group> groups;
//setters and getters...
}
for the Group
import java.io.Serializable;
import java.util.List;
public class Group implements Serializable {
private String id;
private String box;
private String operator;
private List<Mail> mailItems;
//setters and getters...
}
and for the Mail Item
import java.io.Serializable;
public class Mail implements Serializable {
private String numMail;
//setters and getters...
}
to setup XStream for this object the following configuration could be used
XStream xstream = new XStream(new StaxDriver());
xstream.alias("mail", Mail.class);
xstream.useAttributeFor(Mail.class,"numMail");
xstream.alias("group", Group.class);
xstream.useAttributeFor(Group.class, "box");
xstream.useAttributeFor(Group.class, "id");
xstream.useAttributeFor(Group.class, "operator");
xstream.addImplicitCollection(Group.class, "mailItems");
xstream.alias("rootDoc", Root.class);
xstream.addImplicitCollection(Root.class, "groups");
and to use it to read the document
Root result = (Root) xstream.fromXML(...source);
now you can use the Root POJO as you would any parent object and retrieve the corresponding Groups and Mail Items.