Friday, 17 October 2014

First DOM Application


I am going to explain simple DOM Application.

Parsing xml file using DOM parser is a 3 step process.

Step 1: Get the instance of BuilderFactory class.
Ex: DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();

Step 2: Instantiate DocumentBuilder object.
Ex: DocumentBuilder docBuilder = builder.newDocumentBuilder();

Step 3: Get the Document object using the parse method of DocumentBuilder instance.
Document document = docBuilder.parse("employee.xml");

Employee.xml
<?xml version="1.0"?>
<employees>

 <employee id="1">
  <firstname>Hari Krishna</firstname>
  <lastname>Gurram</lastname>
  <address>
   <area>Marthali</area>
   <city>Bangalore</city>
   <state>Karnataka</state>
   <country>India</country>
  </address>
 </employee>
 
 <employee id="2">
  <firstname>Joel Babu</firstname>
  <lastname>Chelli</lastname>
  <address>
   <area>Jubli Hills</area>
   <city>Hyderababd</city>
   <state>Andhra Pradesh</state>
   <country>India</country>
  </address>
 </employee>
 
 <employee id="3">
  <firstname>Susantha</firstname>
  <lastname>Sarm</lastname>
  <address>
   <area>Gandhi Nagar</area>
   <city>Bhuvaneswar</city>
   <state>Orissa</state>
   <country>India</country>
  </address>
 </employee>

</employees>

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParser {

    static Document parseDoc(String doc) throws ParserConfigurationException, SAXException, IOException{
        /* 1. Get the instance of BuilderFactory class. */
        DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();

        /* 2. Instantiate DocumentBuilder object. */
        DocumentBuilder docBuilder = builder.newDocumentBuilder();

        /* Get the Document object  */
        Document document = docBuilder.parse("employee.xml");

        return document;
    }

    public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException{
        Document document = parseDoc("employee.xml");

        System.out.println("Root element :" + document.getDocumentElement().getNodeName());
        System.out.println("Employee Details");

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

        for (int i = 0; i < nList.getLength(); i++) {
            Node nNode = nList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.print(eElement.getElementsByTagName("firstname").item(0).getTextContent()+"\t");
                System.out.print(eElement.getElementsByTagName("lastname").item(0).getTextContent()+"\t");
                System.out.print(eElement.getElementsByTagName("area").item(0).getTextContent()+"\t");
                System.out.print(eElement.getElementsByTagName("city").item(0).getTextContent()+"\t");
                System.out.print(eElement.getElementsByTagName("state").item(0).getTextContent()+"\t");
                System.out.println(eElement.getElementsByTagName("country").item(0).getTextContent());
            }
        }
    }
}


Output
Root element :employees
Employee Details
Hari Krishna        Gurram        Marthali        Bangalore        Karnataka        India
Joel Babu        Chelli        Jubli Hills        Hyderababd        Andhra Pradesh        India
Susantha        Sarm        Gandhi Nagar        Bhuvaneswar        Orissa        India

 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment