Friday, 2 September 2016

DOM Parser

                                                       DOM Parser

DOM – Document Object Model
It Parse the document by loading the complete contents of the xml document along with its hierarchical tree in memory
We should use DOM parser when we need to learn about document Structure ,It provides an interface which enables the programs to access and update the style, structure and contents of XML documents. XML parsers that support the DOM will implements that interface.
XML File
<?xml version="1.0"?>
<company>
   <employee empid="393">
      <firstname>shashank</firstname>
      <lastname>bhatnagar</lastname>
      <nickname>shashank</nickname>
      <salary>85000</salary>
   </employee>
   <employee empid="493">
      <firstname>Raj</firstname>
      <lastname>Gupta</lastname>
      <nickname>Raju</nickname>
      <salary>95000</salary>
   </employee>
   <employee empid="593">
      <firstname>Deepak</firstname>
      <lastname>Kumar</lastname>
      <nickname>Deep</nickname>
      <salary>90000</salary>
   </employee>
</company>

Document Object Model Interfaces :
Node :  The base datatype of the model. 
  • In above example root node is company


Elements :  The vast majority of the objects .

ATTR :  It will represent an attribute of an element
  • In above example we will consider attribute as empid


Text :  The actual content of an element or an attribute

Document :  Represent entire XML document.

Common Document object model methods:
  1. Document.getDocumentElement() – it will return the root element of the document.
  2. Node.getFirstChild() – It will return the first child of a given Node.
  3. Node.getLastChild() – It will return the last child of a given Node.
  4. Node.getNextSibling() – It will return the next sibling of a given Node.
  5. Node.getPreviousSibling() – It will return the previous sibling of a given Node.
  6. Node.getAttribute(attrName) – it willl returns the attribute with the requested name for a given node.
Dom Parser steps :
Following are the steps used while parsing a document using DOM Parser.
1.   Import XML-related packages.
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
1.    Create a DocumentBuilder
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbbuilder = dbfactory.newDocumentBuilder();
2.    Create a Document from a file or stream
File inputFile = new File("D://Articles//XML//company.xml");
Document doc = DBbuilder.parse(inputFile);
doc.getDocumentElement().normalize();
Normalize method : Normalize the root element of the XML document. This ensures that all Text nodes under the root node are put into a "normal" form, which means that there are neither adjacent Text nodes nor empty Text nodes in the document.
3.    Extract the root element
Element root = doc.getDocumentElement();
System.out.println("Root Element is " +  root.getNodeName());
Examine attributes
//returns specific attribute ---- getAttribute("attributeName");
System.out.println("Emplyee id :-" + element.getAttribute("empid"));
//returns a Map (table) of names/values --- getAttributes();
4.    Examine sub-elements
            //returns a list of subelements of specified name

NodeList nList = doc.getElementsByTagName("employee");

System.out.println("Salary is :-" + element.getElementsByTagName("salary").item(0).getTextContent());
The node list keeps itself up-to-date. If an element is deleted or added, in the node list or the XML document, the list is automatically updated.
Note: In a node list, the nodes are returned in the order in which they are specified in the XML document.

getChildNodes(); -- //returns a list of all child nodes 

                Java Program to read above XML
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package domxmlparser;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author shashank
 */
public class Domxmlparser {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try
        {
            File inputFile = new File("D://Articles//XML//company.xml");
            DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder DBbuilder = DBfactory.newDocumentBuilder();
            Document doc = DBbuilder.parse(inputFile);
            Element root = doc.getDocumentElement();
            System.out.println("Root Element is " +  root.getNodeName());
             NodeList nList = doc.getElementsByTagName("employee");
             System.out.println("*******");
             for(int temp=0;temp<nList.getLength();temp++)
             {
                 //Get the current node
                 Node Currentnode = nList.item(temp);
                 System.out.println("Current Element is :" + Currentnode.getNodeName());
                 if(Currentnode.getNodeType()==Node.ELEMENT_NODE)/*getNodeType()A code representing the type of the underlying object, as defined above.*/
                 {
                     Element element = (Element)Currentnode;
System.out.println("Emplyee id :-" + element.getAttribute("empid"));
   
System.out.println("First name is :-" + element.getElementsByTagName("firstname").item(0).getTextContent());
                    
System.out.println("Last name is :-" + element.getElementsByTagName("lastname").item(0).getTextContent());
                     
System.out.println("Nick name is :-" + element.getElementsByTagName("nickname").item(0).getTextContent());
                     
System.out.println("Salary is :-" + element.getElementsByTagName("salary").item(0).getTextContent());
                 }
             }
        }catch(Exception e){e.printStackTrace();}
    }
   


}


No comments:

Post a Comment