使用XStream将Java对象序列化为XML

时间:2021-01-18 21:46:20

The problem is that every time I execute the main method, the old content of a.xml is lost and is substituted with a new one. How to append content to the a.xml file without losing the previous information?

问题是每次执行main方法时,a.xml的旧内容都会丢失并被替换为新内容。如何将内容附加到a.xml文件而不丢失以前的信息?

import java.io.FileNotFoundException;
import java.io.PrintWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class Test {
    public static void main(String[] args) throws FileNotFoundException {
        XStream xs = new XStream(new DomDriver());
        Foo f = new Foo(1, "booo", new Bar(42));
        PrintWriter pw = new PrintWriter("a.xml");
        xs.toXML(f,pw);
    }
}


public class Bar {
    public int id;

    public Bar(int id) {
        this.id = id;
    }

}


public class Foo {
    public int a;
    public String b;
    public Bar boo;
    public Foo(int a, String b, Bar c) {
        this.a = a;
        this.b = b;
        this.boo = c;
    }
}

2 个解决方案

#1


2  

Sample Code

public static void main(String a[]){
  //Other code omitted
  FileOutputStream fos = new FileOutputStream("c:\\yourfile",true); //true specifies append
  Foo f = new Foo(1, "booo", new Bar(42));
  xs.toXML(f,fos);
}

#2


3  

The question is, do you really want to append the serialized XML string to the file or do you want to add the new Foo instance to the XML structure.

问题是,您真的想要将序列化的XML字符串附加到文件中,还是要将新的Foo实例添加到XML结构中。

Appending on a string basis would result in invalid XML about like this:

以字符串为基础附加将导致无效的XML如下:

<foo>
  <a>1</a>
  <b>booo</b>
  <bar>
    <id>42</id>
  </bar>
</foo>
<foo>
  <a>1</a>
  <b>booo</b>
  <bar>
    <id>42</id>
  </bar>
</foo>

Instead you may want to preserve the data in a.xml by parsing it first, then add the new element and serialize the whole collection/array.

相反,您可能希望首先解析它来保存a.xml中的数据,然后添加新元素并序列化整个集合/数组。

So something like this (assuming there is already a collection of Foos in a.xml):

这样的事情(假设在a.xml中已经有一组Foos):

List foos = xs.fromXml(...);
foos.add(new Foo(1, "booo", new Bar(42)));
xs.toXml(foos, pw);

... which gives you something along the lines of this:

...这给你的东西是这样的:

<foos>
  <foo>
    <a>1</a>
    <b>booo</b>
    <bar>
      <id>42</id>
    </bar>
  </foo>
  <foo>
    <a>1</a>
    <b>booo</b>
    <bar>
      <id>42</id>
    </bar>
  </foo>
</foos>

HTH

#1


2  

Sample Code

public static void main(String a[]){
  //Other code omitted
  FileOutputStream fos = new FileOutputStream("c:\\yourfile",true); //true specifies append
  Foo f = new Foo(1, "booo", new Bar(42));
  xs.toXML(f,fos);
}

#2


3  

The question is, do you really want to append the serialized XML string to the file or do you want to add the new Foo instance to the XML structure.

问题是,您真的想要将序列化的XML字符串附加到文件中,还是要将新的Foo实例添加到XML结构中。

Appending on a string basis would result in invalid XML about like this:

以字符串为基础附加将导致无效的XML如下:

<foo>
  <a>1</a>
  <b>booo</b>
  <bar>
    <id>42</id>
  </bar>
</foo>
<foo>
  <a>1</a>
  <b>booo</b>
  <bar>
    <id>42</id>
  </bar>
</foo>

Instead you may want to preserve the data in a.xml by parsing it first, then add the new element and serialize the whole collection/array.

相反,您可能希望首先解析它来保存a.xml中的数据,然后添加新元素并序列化整个集合/数组。

So something like this (assuming there is already a collection of Foos in a.xml):

这样的事情(假设在a.xml中已经有一组Foos):

List foos = xs.fromXml(...);
foos.add(new Foo(1, "booo", new Bar(42)));
xs.toXml(foos, pw);

... which gives you something along the lines of this:

...这给你的东西是这样的:

<foos>
  <foo>
    <a>1</a>
    <b>booo</b>
    <bar>
      <id>42</id>
    </bar>
  </foo>
  <foo>
    <a>1</a>
    <b>booo</b>
    <bar>
      <id>42</id>
    </bar>
  </foo>
</foos>

HTH