1

The following code doesn't work. I got exception when I run these line[Object objPoints = serializer.read(Point.class, file);] In details, the source attachment doesn't contain the source for the Persister.class.

import org.simpleframework.xml.Serializer; 
import org.simpleframework.xml.core.Persister;
import java.io.File;

Serializer serializer = new Persister();
File file = new File("1.xml");
Object objPoints = serializer.read(Point.class, file);

Point Class is as follow :

@Root
public class Point {
   @Element
   private float X;
   @Element
   private float Y;
}

I references from this link.

9
  • What is the library you use? What's the Serializer class? Commented Jan 2, 2013 at 10:38
  • Also i am almost convinced the method will be called read not rea and file will be passed in without the leading >. Make sure you post your real code, please. Commented Jan 2, 2013 at 10:39
  • @Boris,I import [import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister;] Commented Jan 2, 2013 at 10:42
  • What does "Serialization doesn't work." mean? Are you getting an exception? Commented Jan 2, 2013 at 10:44
  • Also include the content of the xml file you are trying to parse Commented Jan 2, 2013 at 10:45

1 Answer 1

1

If i use the xml from your comment and your code it works:

XML:

<Point>
   <X>2</X>
   <Y>3</Y>
</Point>

But in the title of this question you talk about "object list" - can you be more detailed here?

If you need do deserialize a list of Points you cant use the Point class but a list.

Btw. does your xml file contain any additional content or only this one point?

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

2 Comments

Really? I don't know what's the problem. My code does work for me. Actually I need to read the list of object. But I couldn't read one object. After reading one obeject successfully, I wonna try the list of object.
You can't read only one Point out of a XML Point List (eg. <points><point>..</point><point>..</point></points>). But what you can do is creating a PointList class which contains a list of Points. Such a list (incl. all objects in it) can be deserialized.

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.