Wednesday, 29 May 2013

Reading XML file in Java

There are number of way (like DOM, SAX Parser etc.) available in Java which does read XML efficiently, Here I am reading this using normal Java XML parsing using org.w3c.dom parser.


Following is the XML(employee.xml) file that used.




    
        A
        B
        C
        10000
    
    
        D
        E
        F
        20000
    
    
        G
        H
        I
        20000
    



Following is java source code used to parse (employee.xml).


package com.xml.reader;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XmlRead {

    public static void main(String[] args) {
        XmlRead xmlRead = new XmlRead();
        File file = new File("your xml path/employee.xml");
        xmlRead.readEmployeeDetailes(file);
    }

    private void readEmployeeDetailes(File file) {

        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            if (file.exists()==true) {

                org.w3c.dom.Document document = builder.parse(file);
                Element element = document.getDocumentElement();

                System.out.println("Node: \t  \t" +element.getNodeName()); // display root node employee

                NodeList node = element.getElementsByTagName("staff"); // List all element under a node staff

                if (node != null && node.getLength() > 0) {

                    showListOfEmployeeDetailes(node);

                }
            }

        } catch (ParserConfigurationException ex) {
                ex.printStackTrace();
        } catch (SAXException ex) {
                ex.printStackTrace(); 
        } catch (IOException ex) {
               ex.printStackTrace();
        }


    }

//-- Method iterator size of NodeList show all containing detail  

    private void showListOfEmployeeDetailes(NodeList node) {

        for (int i = 0; i < node.getLength(); i++) {
            
            Node node1 = node.item(i);
            
            if (node1.getNodeType() == Node.ELEMENT_NODE) {

                Element element = (Element) node1;

                System.out.println("-------------------------------------------------");

                NodeList nodeList = element.getElementsByTagName("firstname");
                 System.out.println("First name : \t" + nodeList.item(0).getChildNodes().item(0).getNodeValue());


                nodeList = element.getElementsByTagName("lastname");
                 System.out.println("Last name : \t" + nodeList.item(0).getChildNodes().item(0).getNodeValue());

                nodeList = element.getElementsByTagName("nickname");
                 System.out.println("Nick name : \t" + nodeList.item(0).getChildNodes().item(0).getNodeValue());

                nodeList = element.getElementsByTagName("salary");
                 System.out.println("Salary : \t" + nodeList.item(0).getChildNodes().item(0).getNodeValue());

            }
        }
    }
}