[英]Save XML file with XStream
I use XStream
to write an object in a xml file
. 我使用
XStream
在xml file
写入对象。
Then I deserializing the file again to use the objects. 然后,我再次反序列化文件以使用对象。
My Problem is, that after I close my program, the xml "file" is gone. 我的问题是,关闭程序后,xml“文件”消失了。 So how can I save this xml file to a specific directory?
那么如何将这个xml文件保存到特定目录? I already tried
FileOutputStream
but it doesn't work... I also google it, but found not the right solution for me... 我已经尝试过
FileOutputStream
但是它不起作用...我也用google搜索了它,但是找不到适合我的解决方案...
Method savePerson 方法savePerson
public void savePerson(String uNummer, Person person) {
System.out.println("save person");
try{
xml = xstream.toXML(person);
}catch (Exception e){
System.err.println("Error in XML Write: " + e.getMessage());
}
}
And the Method readPerson 以及方法readPerson
public Person readPerson(String uNummer) {
System.out.println("read person");
Person person = new Person();
try{
person = (Person) xstream.fromXML(file_path + uNummer + ".xml");
}catch(Exception e){
System.err.println("Error in XML Read: " + e.getMessage());
}
return person;
}
Directory : \\\\releasearea\\ToolReleaseArea\\PersistenceSave
目录 :
\\\\releasearea\\ToolReleaseArea\\PersistenceSave
EDIT 编辑
Correct Code : (by ppeterka ) 正确的代码 :( ppeterka提供 )
public void savePerson(String uNummer, Person person) {
System.out.println("save person XML");
FileOutputStream fos = null;
try{
xml = xstream.toXML(person);
fos = new FileOutputStream(file_path + uNummer + ".xml");
fos.write("<?xml version=\"1.0\"?>".getBytes("UTF-8"));
byte[] bytes = xml.getBytes("UTF-8");
fos.write(bytes);
}catch (Exception e){
System.err.println("Error in XML Write: " + e.getMessage());
}
finally{
if(fos != null){
try{
fos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
You didn't write the file, just obtained the serialized content... 您没有写文件,只是获得了序列化的内容...
FileOutputStream fos = null;
try {
fos = new FileOutputStream("myfilename");
fos.write("<?xml version=\"1.0\"?>".getBytes("UTF-8")); //write XML header, as XStream doesn't do that for us
byte[] bytes = xml.getBytes("UTF-8");
fos.write(bytes);
} catch(Exception e) {
e.printStackTrace(); // this obviously needs to be refined.
} finally {
if(fos!=null) {
try{
fos.close();
} catch (IOException e) {
e.printStackTrace(); // this obviously needs to be refined.
}
}
}
Also, your reading function has an error too: the xstream.fromXML(String)
accepts a String
, but it does not interpret it as a file name, but as the XML content itself... You have to use the fromXML(File)
function: 另外,您的阅读函数也有一个错误:
xstream.fromXML(String)
接受一个String
,但它不会将其解释为文件名,而是将其解释为XML内容本身...您必须使用fromXML(File)
功能:
public Person readPerson(String uNummer) {
System.out.println("read person");
Person person = new Person(); //if there is an error during deserialization, this is going to be returned, is this what you want?
try{
File xmlFile = new File(file_path + uNummer + ".xml");
person = (Person) xstream.fromXML(xmlFile);
}catch(Exception e){
System.err.println("Error in XML Read: " + e.getMessage());
}
return person;
}
Use the overloaded method toXML(Object o, Writer w) to serialize directly to a file. 使用重载的toXML(Object o,Writer w)方法直接序列化到文件。 The toXML method you are using doesn't save to a file.
您使用的toXML方法不会保存到文件中。
xstream.toXML(person, new FileWriter(file));
Im doing like this is working well : 我这样做的效果很好:
//Your Stream things...
String xml = xstream.toXML(type);
System.out.println(xml);
BufferedReader reader = new BufferedReader(new StringReader(xml));
BufferedWriter writer = new BufferedWriter(new FileWriter("test.xml",
true));
while ((xml = reader.readLine()) != null) {
writer.write(xml + System.getProperty("line.separator"));
}
writer.close()
; ;
And output is : 输出为:
<type>
<OBJECT__TYPE>sdfsdf</OBJECT__TYPE>
<prop>
<DESCRIPTION>hfh</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>1</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>345345</HELP>
<MIN__NO>NULL</MIN__NO>
<MAX__NO>1</MAX__NO>
<NAME__FORMAT>NULL</NAME__FORMAT>
</prop>
</type>
<type>
<OBJECT__TYPE>test</OBJECT__TYPE>
<prop>
<DESCRIPTION>test</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>0</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>noe</HELP>
<MIN__NO>NULL</MIN__NO>
<MAX__NO>NULL</MAX__NO>
<NAME__FORMAT>5475</NAME__FORMAT>
</prop>
</type>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.