The basis
to work with an XML document or to create an XML document from the scratch
is to have a document object in place. So, your first task should be
to create a document object using the classes and methods available
for that purpose in Java.
_______________________________________________
Creating
a document object involves three steps. The first is to create a DocumentBuilderFactory
object. The second is to create a DocumentBuilder object with that DocumentBuilderFactory
object. Now in the third step you can create the Document object using
the Document Builder object. A typical code should be,
DocumentBuilderFactory
dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.newDocument();
You can
use the getDocumentElement() method to get the root node and then you
can use the appendChild() method to append the elements created, to
the root node. To create the text nodes, you can use the method createTextNode()
of the document object.
Manipulating
the existing XML document is also possible and there are methods to
traverse the nodes and to change them or to remove them. You can also
traverse the nodes and then create attributes.