DOM

Create empty DOM document

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: June 28th, 2013
0 661 1 minute read

With this example we are going to demonstrate how to create an empty DOMDocument, 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 aDocumentBuilderFactory, 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 aDocumentBuilder, usingnewDocumentBuilder() API method of DocumentBuilderFactory.
  • CallnewDocument() API method of DocumentBuilder to obtain a new instance of a DOM Document object.
  • Get the value of thisNode, usinggetNodeValue() 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.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: June 28th, 2013
0 661 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.