I'm using xStream to manipulate XML. All is okay. To put on XML archive and other things. But, I have a problem:
Example: My xml contains a tag , and inside this one, I have some more tags named <comment>
. Look at a example code:
<comments>
<comment>
<id>1</id>
<desc>A comment</desc>
</comment>
<comment>
<id>2</id>
<desc>Another comment</desc>
</comment>
<comment>
<id>3</id>
<desc>Another one comment</desc>
</comment>
</comments>
And progressivelly. I can do 500 tags inside the tag. And these comments are of type comment.
How I can do to serialize with the xStream to put all of these tags in the classes? I don't how to make in the class to it receive the various objects.
Obviously, I will make this with an Array, or some other. But I don't know how I can do this.
For that XML, you'd probably be looking to have a class structure like:
public class Comment {
long id
String desc
}
public class Comments {
List<Comment> comments = new ArrayList<Comment>();
}
Your unmarshalling logic would then be something like:
XStream xstream = new XStream();
xstream.alias("comments", Comments.class);
xstream.alias("comment", Comment.class);
xstream.addImplicitCollection(Comments.class, "comments");
Comments comments = (Comments)xstream.fromXML(xml);
Additionaly, as Nishan mentioned in the comments, your XML isn't quite formed correctly. You'll need to make sure your <comment>
ends with </comment>
and not </comments>
. You'll need to fix this before any of the code in this answer will work.