使用 FileInputStream 读取 XML 文件(对于 Java)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/361570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading an XML File using FileInputStream (for Java)?
提问by ParseTheData
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!
编辑:嘿伙计们,感谢您的帮助,我想通了;我不认为我必须将它打印为字符串,我只需要制作一个基准测试框架来计时等等,但再次感谢!
回答by Marc Novakowski
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 (RandomTreeI 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 库
回答by Cambium
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),您需要先定义您的类型。强制转换为 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
要将您的对象输出为字符串,只需执行
String xml = xstream.toXML( myRandomTree );

