How to read simple XML file using DOM Parser

In this article we are going to see about how to read simple xml file. If your xml file has space or any special character then you need to normalize your xml file . Here I iterate the nodes one by one using DOM parser

ReadXML.java

package com.ehowtonow.xml.dom;

import java.io.File;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXML {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  try {
   File xmlFile = new File("C:\\jtc\\student.xml");
   DocumentBuilderFactory builderFactory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder builder = builderFactory.newDocumentBuilder();
   Document document = builder.parse(xmlFile);
   System.out.println("Root Node :"
     + document.getDocumentElement().getNodeName());

   if (document.hasChildNodes()) {
    NodeList list = document.getChildNodes();
    nodeIterator(list);

   }
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 private static void nodeIterator(NodeList list) {

  for (int i = 0; i < list.getLength(); i++) {
   Node node = list.item(i);

   if (node.getNodeType() == Node.ELEMENT_NODE) {
    System.out.println("**********************");
    System.out.println("Element Node : " + node.getNodeName()
      + " [Tag Open]");
    System.out.println("Text Value : " + node.getTextContent());

    if (node.hasAttributes()) {
     NamedNodeMap map = node.getAttributes();
     for (int j = 0; j < map.getLength(); j++) {
      Node node2 = map.item(j);
      System.out.println("Attribute Name : "
        + node2.getNodeName());
      System.out.println("Attribute Value : "
        + node2.getNodeValue());

     }
    }
    if (node.hasChildNodes()) {
     nodeIterator(node.getChildNodes());
    }
    System.out.println("Element Node : " + node.getNodeName()
      + " [Tag Closed]");
    System.out.println("**********************");
   }

  }
 }
}

student.xml

<?xml version="1.0" encoding="UTF-8"?>
<college>
<student id="1">
<firstname>Shiva</firstname>
<lastname>Kumar</lastname>
<department>ECE</department>
</student>
<student id="2">
<firstname>Appu</firstname>
<lastname>T</lastname>
<department>CSE</department>
</student>
</college>

Output


Root Node :college
**********************
Element Node : college [Tag Open]


Text Value :


Shiva


Kumar


ECE


Appu


T


CSE


**********************


Element Node : student [Tag Open]


Text Value :


Shiva


Kumar


ECE


Attribute Name : id


Attribute Value : 1


**********************


Element Node : firstname [Tag Open]


Text Value : Shiva


Element Node : firstname [Tag Closed]


**********************


**********************


Element Node : lastname [Tag Open]


Text Value : Kumar


Element Node : lastname [Tag Closed]


**********************


**********************


Element Node : department [Tag Open]


Text Value : ECE


Element Node : department [Tag Closed]


**********************


Element Node : student [Tag Closed]


**********************


**********************


Element Node : student [Tag Open]


Text Value :


Appu


T


CSE


Attribute Name : id


Attribute Value : 2


**********************


Element Node : firstname [Tag Open]


Text Value : Appu


Element Node : firstname [Tag Closed]


**********************


**********************


Element Node : lastname [Tag Open]


Text Value : T


Element Node : lastname [Tag Closed]


**********************


**********************


Element Node : department [Tag Open]


Text Value : CSE


Element Node : department [Tag Closed]


**********************


Element Node : student [Tag Closed]


**********************


Element Node : college [Tag Closed]


**********************

Output

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. How to Count number of Element in XML
  2. How to Modify XML file using DOM Parser
  3. Java DOM XML Parser
  4. How to Read XML File using DOM parser
  5. How to read simple XML file using DOM Parser
  6. How to create XML file using DOM Parser

Be the first to comment

Leave a Reply