Pages

Sunday 23 June 2013

Read XML file using Java

Today i will so you how to read XML file data using Java so that you can further use for other purpose.

First of all you need xmlparserv2.jar  library file that is easily downloadable on net.

This is a XML file look like:

<student>
<person>
  <first>Anand</first>
  <last>Maheshwari</last>
  <age>22</age>
</person>
<person>
  <first>Vivek</first>
  <last>Shah</last>
  <age>21</age>
</person>
<person>
  <first>Steve</first>
  <last>Jobs</last>
  <age>40</age>
</person>
</student>

This is Java code for read XML :

package org.anand;

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;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ReadXMLFile {

    public static void main(String[] args) {
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("D:/test.xml"));

            // Normalize text representation...
            doc.getDocumentElement().normalize();
            System.out.println("Root element of the doc is : "
                    + doc.getDocumentElement().getNodeName());
            NodeList listOfPersons = doc.getElementsByTagName("person");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of person : " + totalPersons);

            for (int i = 0; i < listOfPersons.getLength(); i++) {
                Node firstPersonNode = listOfPersons.item(i);
                if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element firstPersonElement = (Element) firstPersonNode;

                    // ----------------
                    NodeList firstNameList = firstPersonElement
                            .getElementsByTagName("first");
                    Element firstNameElement = (Element) firstNameList.item(0);

                    NodeList textFNList = firstNameElement.getChildNodes();
                    System.out
                            .println("First Name : "
                                    + ((Node) textFNList.item(0))
                                            .getNodeValue().trim());

                    // ----------------
                    NodeList lastNameList = firstPersonElement
                            .getElementsByTagName("last");
                    Element lastNameElement = (Element) lastNameList.item(0);

                    NodeList textLNList = lastNameElement.getChildNodes();
                    System.out
                            .println("Last Name : "
                                    + ((Node) textLNList.item(0))
                                            .getNodeValue().trim());

                    // ----------------
                    NodeList ageList = firstPersonElement
                            .getElementsByTagName("age");
                    Element ageElement = (Element) ageList.item(0);

                    NodeList textAgeList = ageElement.getChildNodes();
                    System.out.println("Age : "
                            + ((Node) textAgeList.item(0)).getNodeValue()
                                    .trim());

                    // ---------------

                } // End of if clause
            }// end of for loop
        } catch (SAXParseException perr) {
            System.out.println("** parsing error " + ", line"
                    + perr.getLineNumber() + ", uri " + perr.getSystemId());
            System.out.println(" " + perr.getMessage());
        } catch (SAXException se) {
            Exception x = se.getException();
            ((x == null) ? se : x).printStackTrace();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }// end of main
} // end of class

Output Of This program :

Root element of the doc is : student
Total no of person : 3
First Name : Anand
Last Name : Maheshwari
Age : 22
First Name : Vivek
Last Name : Shah
Age : 21
First Name : Steve
Last Name : Jobs
Age : 40

No comments:

Post a Comment