使用XStream保存XML文件

时间:2012-10-25 07:40:02

标签: java xstream

我使用XStreamxml file中编写对象。
然后我再次反序列化文件以使用对象。
我的问题是,在我关闭程序后,xml“文件”消失了。那么如何将此xml文件保存到特定目录?我已经尝试了FileOutputStream但是它不起作用...我也谷歌了,但发现不适合我...

方法 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());
    }
}

方法 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;
}

目录\\releasearea\ToolReleaseArea\PersistenceSave

编辑
正确代码 :(通过 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();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:5)

你没有写文件,只是获得了序列化的内容......

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.
        }
    }
}

此外,您的阅读功能也有错误: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;
}

答案 1 :(得分:5)

使用重载方法toXML(Object o,Writer w)直接序列化到文件。 您使用的toXML方法不保存到文件。

xstream.toXML(person, new FileWriter(file));

答案 2 :(得分:0)

我这样做很有效:

//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()

输出是:

<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>