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).
对于我的项目,我必须使用Java和XStream对随机树进行序列化和反序列化。我的老师制作了Tree / RandomTree算法,所以我不必担心。我不知道怎么做是这样的:我使用FileInputStream来读/写我序列化和反序列化的xml文件,但是当我反序列化时,我不知道用于读取文件的方法。在我读取文件后,我应该能够从XML转换它,然后将其作为字符串打印出来。这是我到目前为止所拥有的。 (我正确导入了所有内容,只是没有将它添加到我的代码段)。
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!
编辑:嘿伙计们,谢谢你的帮助,我想出来了;我不认为我必须将其打印为字符串,我只需要制作一个基准测试框架来计时等等,但再次感谢!
2 个解决方案
#1
1
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:
xstream.fromXML()方法将为您输入输入流。我认为问题是你将xstream.fromXML(fin)的返回值转换为String,因为它应该转换为你最初序列化的对象类型(我假设为RandomTree)。所以代码看起来像这样:
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
编辑:在评论中澄清后,作者的目标是首先读入一个字符串,以便在反序列化之前打印XML内容。考虑到这一目标,我建议您查看此主题中提到的IOUtils库
#2
1
From what I understand from http://x-stream.github.io/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:
根据我的理解http://x-stream.github.io/tutorial.html(我以前从未使用过XStream),您需要先定义类型。 Casting to String绝对是错误的,您可能需要自定义类型(取决于随机XML中的内容),然后您需要将XML标记映射到您的成员:
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.
这意味着它将XML中的“person”标记映射到Person类。
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 :)
此外,你关闭你的流两次,你可能想在finally块:)
edit: Having read your comment above...
编辑:阅读上面的评论......
Your task involves two steps:
您的任务涉及两个步骤:
- Deserialization
- Serialization
In order to serialize your object, you must deserialize it first from your input file.
要序列化对象,必须首先从输入文件中反序列化它。
To output your Object as String, simply do
要将Object输出为String,只需执行
String xml = xstream.toXML( myRandomTree );
#1
1
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:
xstream.fromXML()方法将为您输入输入流。我认为问题是你将xstream.fromXML(fin)的返回值转换为String,因为它应该转换为你最初序列化的对象类型(我假设为RandomTree)。所以代码看起来像这样:
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
编辑:在评论中澄清后,作者的目标是首先读入一个字符串,以便在反序列化之前打印XML内容。考虑到这一目标,我建议您查看此主题中提到的IOUtils库
#2
1
From what I understand from http://x-stream.github.io/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:
根据我的理解http://x-stream.github.io/tutorial.html(我以前从未使用过XStream),您需要先定义类型。 Casting to String绝对是错误的,您可能需要自定义类型(取决于随机XML中的内容),然后您需要将XML标记映射到您的成员:
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.
这意味着它将XML中的“person”标记映射到Person类。
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 :)
此外,你关闭你的流两次,你可能想在finally块:)
edit: Having read your comment above...
编辑:阅读上面的评论......
Your task involves two steps:
您的任务涉及两个步骤:
- Deserialization
- Serialization
In order to serialize your object, you must deserialize it first from your input file.
要序列化对象,必须首先从输入文件中反序列化它。
To output your Object as String, simply do
要将Object输出为String,只需执行
String xml = xstream.toXML( myRandomTree );