woodhead92 woodhead92 - 1 month ago 5x
Java Question

Creating an XML document using Java

I have seen many programs where XML documents can be created using Java with specified fields. I am yet to come across one where the user gets to decide the names of the rootElement's and the childEelement's. Does anyone know how to go about this?


Use XStream

Java Code

XStream xstream = new XStream();
xstream.alias("person", Person.class);
xstream.alias("phonenumber", PhoneNumber.class);

Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));

String xml = xstream.toXML(joe);

Output XML


Library: http://x-stream.github.io/tutorial.html