DOM
Create empty DOM document
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, 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 thisNode, 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.
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.
Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.



