public class Person {
private String name;
private int age;
private String address;
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
// Getters and setters
// ...
}
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.15</version>
</dependency>
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class Main {
public static void main(String[] args) {
// Create a Person object
Person person = new Person("John Doe", 30, "123 Street, City");
// Create an instance of XStream
XStream xstream = new XStream(new DomDriver());
// Convert the Person object to XML
String xml = xstream.toXML(person);
// Save the XML to a file
try {
FileWriter fileWriter = new FileWriter("person.xml");
fileWriter.write(xml);
fileWriter.close();
System.out.println("Person object was successfully converted to XML and saved to file.");
} catch (IOException e) {
System.out.println("Error occurred while saving the XML file.");
e.printStackTrace();
}
}
}