In this tutorial you will learn about the parsing XML document using DOM
Parsing XML Document
We have used following XML file in example
student.xml
<?xml version="1.0"?> <SCHOOL> <STUDENT> <ROLL>01</ROLL> <NAME>Tom</NAME> <AGE>22</AGE> <COURSE>MBA</COURSE> <ADDRESS>Delhi</ADDRESS> </STUDENT> <STUDENT> <ROLL>02</ROLL> <NAME>Rao</NAME> <AGE>21</AGE> <COURSE>BBA</COURSE> <ADDRESS>Ranchi</ADDRESS> </STUDENT> <STUDENT> <ROLL>03</ROLL> <NAME>Singh</NAME> <AGE>23</AGE> <COURSE>MBA</COURSE> <ADDRESS>Patna</ADDRESS> </STUDENT> <STUDENT> <ROLL>04</ROLL> <NAME>Amoo</NAME> <AGE>22</AGE> <COURSE>MBA</COURSE> <ADDRESS>Lucknow</ADDRESS> </STUDENT> </SCHOOL>
To parse XML document do the following
- Create a file Object as File xmlFile = new File("G:\\xmlfiles\\student.xml");
- Create DocumentBuilderFactory instance and get DocumentBuilder instance from it.
- Then normalize the document and get the NodeList of the STUDENT element as follows
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder(); Document document = documentBuilder.parse(xmlFile); document.getDocumentElement().normalize(); NodeList nodeList = document.getElementsByTagName("STUDENT");
for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; NodeList rollNodeList = element.getElementsByTagName("ROLL"); Element roll = (Element) rollNodeList.item(0); NodeList rollNode = roll.getChildNodes(); System.out.println("Roll : " + ((Node) rollNode.item(0)).getNodeValue()); } }
The Complete exaple is given below
DomXmlParsingExample.java
package dom; 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; public class DomXmlParsingExample { public static void main(String argv[]) { try { File xmlFile = new File("G:\\xmlfiles\\student.xml"); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder(); Document document = documentBuilder.parse(xmlFile); document.getDocumentElement().normalize(); NodeList nodeList = document.getElementsByTagName("STUDENT"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; NodeList rollNodeList = element.getElementsByTagName("ROLL"); Element roll = (Element) rollNodeList.item(0); NodeList rollNode = roll.getChildNodes(); System.out.println("Roll : " + ((Node) rollNode.item(0)).getNodeValue()); NodeList nameNodeList = element.getElementsByTagName("NAME"); Element name = (Element) nameNodeList.item(0); NodeList nameNode = name.getChildNodes(); System.out.println("NAME : " + ((Node) nameNode.item(0)).getNodeValue()); NodeList ageNodeList = element.getElementsByTagName("AGE"); Element age = (Element) ageNodeList.item(0); NodeList ageNode = age.getChildNodes(); System.out.println("AGE : " + ((Node) ageNode.item(0)).getNodeValue()); NodeList courseNodeList = element.getElementsByTagName("COURSE"); Element course = (Element) courseNodeList.item(0); NodeList courseNode = course.getChildNodes(); System.out.println("COURSE : " + ((Node) courseNode.item(0)).getNodeValue()); NodeList addressNodeList = element.getElementsByTagName("ADDRESS"); Element address = (Element) addressNodeList.item(0); NodeList addressNode = address.getChildNodes(); System.out.println("ADDRESS : "+((Node) addressNode.item(0)).getNodeValue()); } } } catch (Exception e) { e.printStackTrace(); } } }
When you run this application it will display message as shown below:
Roll : 01 NAME : Tom AGE : 22 COURSE : MBA ADDRESS : Delhi Roll : 02 NAME : Rao AGE : 21 COURSE : BBA ADDRESS : Ranchi Roll : 03 NAME : Singh AGE : 23 COURSE : MBA ADDRESS : Patna Roll : 04 NAME : Amoo AGE : 22 COURSE : MBA ADDRESS : Lucknow |
[ 0 ] Comments