DOM

Create empty DOM document

With this example we are going to demonstrate how to create an empty DOM Document, that represents the entire HTML or XML document. The DOM Document is the root of the document tree, and provides the primary access to the document’s data. In short, to create an empty DOM Document you should:

  • Obtain a new instance of a DocumentBuilderFactory, that is a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
  • Create a new instance of a DocumentBuilder, using newDocumentBuilder() API method of DocumentBuilderFactory.
  • Call newDocument() API method of DocumentBuilder to obtain a new instance of a DOM Document object.
  • Get the value of this Node, using getNodeValue() API method of Node.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class CreateEmptyDOMDocument {
	
	public static void main(String[] args) throws Exception {
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = dbf.newDocumentBuilder();
		Document doc = builder.newDocument();
		System.out.println(doc.getNodeValue());
		
	}

}

Output:

null

  
This was an example of how to create an empty DOM Document in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button