Tuesday 3 December 2013

Simple Way to do DOM parser program

DOM: Document Object Model
A DOM Document is a collection of nodes, or pieces of information, organized in a hierarchy. This hierarchy allows a developer to navigate around the tree looking for specific information.
 Analyzing the structure normally requires the entire document to be loaded and the hierarchy to be built before any work is done. Because it is based on a hierarchy of information, the DOM is said to be tree-based, or object-based.
          DOM create in build memory tree like structure, whenever it starts
Parsing, after completing parsing of entire file that tree throws to application program to handle each and every events occurred in it.



Parsing a file into a document:
It is three-step process. To work with the information in an XML file, the file must be parsed to create a Document object. The Document object is an interface, so it can't be instantiated directly; generally, the application uses a factory instead. The exact process varies from implementation to implementation, but the ideas are the same.
In the example Java environment, parsing the file is a three-step process:
1. Create the DocumentBuilderFactory. This object creates the DocumentBuilder.
2. Create the DocumentBuilder. The DocumentBuilder does the actual parsing to create the Document object.

3. Parse the file to create the Document object.

Now you can start building the application
First, the Java code imports the necessary classes, and then it creates the OrderProcessor application. The Document object can be used later; the application defines it outside the try-catch block. Within the try-catch block, the application creates the DocumentBuilderFactory, which it then uses to create the DocumentBuilder. Finally, the DocumentBuilder parses the file to create the Document.
Once the document is parsed and a Document is created, an application can step through the structure to review, find, or display information. This navigation is the basis for many operations that will be performed on a Document. Stepping through the document begins with the root element. A well-formed document has only one root element, also known as the DocumentElement. First the application retrieves this element.
DOM Example:
Input XML File for parsing

<?xml version="1.0" encoding="UTF-8"?>
<weather>
<day1>
<location>Vita</location>
<date>25/04/2013</date>
<temp>42'c</temp>
</day1>
</weather>

DOM Program for XML File parsing

import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Dom
{
public static void main(String args[])
 {
  try
   {
          File f=new File("weather.xml");
         DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
          DocumentBuilder db=dbf.newDocumentBuilder();
          Document doc=db.parse(f);
     System.out.println("RootNode="+doc.getDocumentElement().getNodeName());
         
     }
   catch(Exception e)
     {    e.printStackTrace();
     }
   }
  }  


How to compile and run Dom parser program.
Save these two programs in same directory Jdk/bin
Compile all programs and run it
Your Output will display.

ShareThis

Related Posts Plugin for WordPress, Blogger...