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]
**********************
0 comments:
Post a Comment