Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 9 November 2013

Read XML file using DOM Parser1


In this tutorials we going to see how to read the XML document and iterate the nodes one by one using DOM parser
1. Create Project Called JavaXML.
2. Create package called com.javatutorialscorner.xml.dom under JavaXML.
3. Create Java class called ReadXML.java under com.javatutorialscorner.xml.dom package.
ReadXML.java
package com.javatutorialscorner.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>

Now you can run the program see the following output in console.

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]

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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Create XML File using DOM ParserIn this tutorials we going to see how to create the XML document using DOM parser 1. Create Project Called JavaXML. 2. Create package called com.javatutorialscorner.xml.dom under JavaXML. 3. Create Java class called CreateXML… Read More
  • Read XML file using DOM Parser1In this tutorials we going to see how to read the XML document and iterate the nodes one by one using DOM parser 1. Create Project Called JavaXML. 2. Create package called com.javatutorialscorner.xml.dom under JavaXML. 3. Cre… Read More
  • Java StAX IntroductionIn this tutorial we are going to see the Java Streaming API for XML (StAX). The StAX is a streaming Java technology-based, event-driven, pull-parsing API for reading and writing XML documents. StAX enables you to create bidr… Read More
  • SAX Parser Introduction SAX – Simple API for XML SAX parser work differently with DOM parser.SAX parser is faster and uses less memory than DOM parser.SAX don’t load and XML document into memory and don’t create any object representation of XM… Read More
  • Read XML File using DOM parserIn this tutorial we are going to see how to read XML file using DOM Parser. In this example I used to read XML document by Element Name.DOM parser parse the entire XML document and load it into memory.DOM parse the nodes as t… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Read XML file using DOM Parser1 Rating: 5 Reviewed By: eHowToNow