main 메소드를 실행할 때마다 a.xml의 이전 컨텐츠가 손실되어 새로운 메소드로 대체된다는 문제점이 있습니다. 이전 정보를 잃지 않고 a.xml 파일에 내용을 추가하는 방법?XStream을 사용하여 Java 객체를 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;
}
}
대단히 감사합니다. –