繁体   English   中英

使用XStream保存XML文件

[英]Save XML file with XStream

我使用XStreamxml file写入对象。
然后,我再次反序列化文件以使用对象。
我的问题是,关闭程序后,xml“文件”消失了。 那么如何将这个xml文件保存到特定目录? 我已经尝试过FileOutputStream但是它不起作用...我也用google搜索了它,但是找不到适合我的解决方案...

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

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

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

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

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

我这样做的效果很好:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM