With Java
you can generate XML in many ways. One of the ways is to write the XML
code using the out.println() statement and generate the XML file you
need using the PrintWriter. However this method is very straightforward
and there is no guarantee that the generated XML file would be error
free.
_______________________________________________
Another
way is to use the Java API for XML Processing. By using this method
you can create a document object and then create the different elements
of the XML file using the Element object. The document object has methods
for this purpose.
Methods
like appendChild() and createTextNode() are used in creating the text
nodes and then adding them to the root node of the XML document.
One another
way to generate XML from Java is to serialize the Java objects in to
XML using the XStream class. With this method you can easily serialized
the Java objects to XML. For example if you have a class 'person', you
can serialize that with the statement,
String
xml = xs.toXML(person);
In the
above statement 'xs' is the XStream object. To work with this object
you have to import com.thoughtworks.xstream.XStream.