8

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.

2
  • 2
    Your xml looks a bit off. <comment> tag should end with </comment> tag, and not </comments> tag. Also, is this what you are looking for : stackoverflow.com/questions/3136234/… ? Commented Mar 27, 2011 at 16:06
  • This question is also similar. Commented Mar 27, 2011 at 17:19

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ooh, yes. Thank you. In this xml that I post here I simple make it manually. The xml is correct. All the tags are closing the correct tags. Thanks for answer.
@Rob Hruska I know is a little late, but how do I specify the XML file location?
@Twhite1195 The XML should be read from a FileInputStream or something.
7

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);

Comments

1

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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.