Saving an XML document
In this section, we will save a DOMDocument
object to an XML document using the DOM 3.0 Load and Save API. We shall use the LSerializer
interface to save the XML document.
Creating a document object
First, create an XMLDOMImplementation
class object. The XMLDOMImplementation
class implements the DOMImplementationLS
interface and has methods to create Load and Save objects.
XMLDOMImplementation impl = new XMLDOMImplementation();
Create an org.w3c.dom.Document
object from the XMLDOMImplementation
object using the createDocument(java.lang.String namespaceURI,java.lang.String qualifiedName, DocumentType doctype)
method. Rather logically, the parameter namespaceURI
specifies the namespace URI of the document element. The parameter qualifiedName
specifies the qualified name of the document element, and the parameter doctype
specifies the DOCTYPE
of the XML document.
Document document = impl.createDocument(null, null, null);
Create and add elements and attributes to the XML...