开发者

xStream problems - How to deserialize multiple objects

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>
   <co开发者_Go百科mment>
      <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.


Although it is an old thread, but here is the Annotated version:

@XStreamAlias("comment")
public class Comment {
    long id
    String desc
}

@XStreamAlias("comments")
public class Comments {
    @XStreamImplicit(itemFieldName = "comment")
    List<Comment> comments;
}

To unmarshal you need this:

XStream xStream = new XStream();
xStream.processAnnotations(new Class[] { Comments.class, Comment.class });
Comments comments = (Comments)xStream.fromXML(xml);


If your dealing with multiple objects, you might be expecting to call fromXML(InputStream in) multiple times to get each object. The method does not handle as expected though and has throws a poorly worded exception message if you do this. Alternatively, wrapping all objects in a larger object may cause the program use more memory then desired or run out of memory.

To fix this, I made a generic utility method so I could parse each small object into its own string so I could fromXML(String) method and still scale up in size.

Example calls:

String element = next(in, "</MyObject>");
MyObject o = (MyObject)xstream.fromXML(element);

public static String next(InputStream in, String occurence) throws IOException {
        StringBuffer sb = new StringBuffer();
        int i;
        int pos = 0;
        while((i = in.read()) != -1) {
            sb.append((char)i);
            if(i == occurence.charAt(pos)) {
                pos ++;
            } else 
                pos = 0;

            if(pos == occurence.length())
                return sb.toString();
        }
        return null;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜