views:

2965

answers:

2

Hi everyone, here's the deal.

For my project I have to serialize and deserialize a random tree using Java and XStream. My teacher made the Tree/RandomTree algorithms, so I don't have to worry about that. What I don't know how to do is this: I am using FileInputStream to read/write the xml file that I serialized and deserialized, but when I deserialize, I do not know the method used to read the file. After I read the file I should be able to convert it from XML and then print it out as a string. Here's what I have so far. (I imported everything correctly, just didn't add it to my code segment).

FileInputStream fin;     

try
{
    // Open an input stream
    fin = new FileInputStream ("/Users/Pat/programs/randomtree.xml");

    //I don't know what to put below this, to read FileInpuStream object fin

    String dexml = (String)xstream.fromXML(fin);

 System.out.println(dexml);

    // Close our input stream
    fin.close(); 


 System.out.println(dexml);

    // Close our input stream
    fin.close();  
}
// Catches any error conditions
catch (IOException e)
{
 System.err.println ("Unable to read from file");
 System.exit(-1);
}


Edit: Hey guys, thanks for the help, I figured it out; I don't think I have to print it as a string, I just needed to make a benchmarking framework to time it and such, but thanks again!

+1  A: 

The xstream.fromXML() method will do the reading from the input stream for you. I think the problem is that you are casting the return value from xstream.fromXML(fin) into a String when it should be cast to the type of object you originally serialized (RandomTree I assume). So the code would look like this:

RandomTree tree = (RandomTree)xstream.fromXML(fin);

EDIT: after clarification in comments, the author's goal is to first read into a String so the XML contents can be printed before deserialization. With that goal in mind, I recommend taking a look at the IOUtils library mentioned in this thread

Marc Novakowski
Well, I wanted to be able to print out what I just read (in a string), so that it would print out both the XML and the deserialized version (in String format). Is that an easy fix? I would assume so, but when I did what you said, I got GeneralTree@af72d8 (where it was in my memory I guess)
ParseTheData
+1  A: 

From what I understand from http://xstream.codehaus.org/tutorial.html (I've never worked with XStream before), you need to define your types first. Casting to String is definitely wrong, you probably want a customized type (depending on what's inside your random XML), then you need to map the XML tags to your members:

e.g.

xstream.alias("person", Person.class);
xstream.alias("phonenumber", PhoneNumber.class);

meaning that it maps the "person" tag inside your XML to your Person class.

To derserialize, you can do:

RandomTree myRandomTree = (RandomTree)xstream.fromXML( xml );

Also, you are closing your stream twice, and you probably want to do it in a finally block :)

edit: Having read your comment above...

Your task involves two steps:

  1. Deserialization
  2. Serialization

In order to serialize your object, you must deserialize it first from your input file.

To output your Object as String, simply do

String xml = xstream.toXML( myRandomTree );
Cambium