Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Xml Dom Creating Blank DOM Document

Related Tutorials


 
 

Share on Google+Share on Google+

Creating Blank DOM Document

Advertisement
This tutorial shows you how to create blank DOM document. JAXP (Java API for XML Processing) is a Java interface that provides a standard approach to Parsing XML documents.

Creating Blank DOM Document

     

This tutorial shows you how to create blank DOM document. JAXP (Java API for XML Processing) is a Java interface that provides a standard approach to Parsing XML documents. With JAXP, we will use the Document BuilderFactory to create DocumentBuilder class.

The class DocumentBuilderFactory is responsible for creating new DOM parsers. Normally it is used to a DOM parser. Example is as follows:


 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder parser = factory.newDocumentBuilder();
 Document doc = parser.parse(myInputSource); //The parse function is used to parse existing xml document.
 

DocumentBuilderFactory uses the system property javax.xml.parsers.XmlDocumentParserFactory to find the class to load. So you can change the parser by calling:
 System.setProperty("javax.xml.parsers.XmlDocumentParserFactory",
  "com.foo.myFactory");

The instance of the class DocumentBuilder is used to create a blank document. The newDocument() method of the class returns a blank DOM document.

Document doc = parser.newDocument();

Here is the full code of CreateBlankDocument.java

import org.w3c.dom.*;

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


public class CreateBlankDocument
{
  public static void main(String[] args)
  {
  System.out.println("Creating Balnk Document...");
  try{
  //Create instance of DocumentBuilderFactory
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  //Get the DocumentBuilder
  DocumentBuilder parser = factory.newDocumentBuilder();
  //Create blank DOM Document
  Document doc = parser.newDocument();
  }catch(Exception e){
  System.out.println(e.getMessage());
  }

  System.out.println("Done...");
  System.out.println("Exiting...");

  }

}

In the next section I will show you how to add root and child elements to the blank document. Saving the DOM tree to the disk file is also discussed in the next secion.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 26, 2008

Related Tutorials

Discuss: Creating Blank DOM Document   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com