A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Wednesday, November 14, 2012

XML Parsing in java - DOM Parser

11:29:00 PM Posted by Satish , No comments
The DOM interface is the easiest XML parser to understand, and use. It parses entire XML document and loads it into memory; then models it with Object for easy traversal or manipulation. DOM Parser is slow and will consume a lot of memory when it loads an XML document which contains a lot of data. Please consider SAX parser as solution for it, SAX is faster than DOM and use less memory.

 JAVA AND XML
                   XML Parsing using Java
                         1. DOM XML Parser
                         2. SAX XML Parser
                         3. StaX XML Parser
                         4. JAXB XML Parser

I have used the following things for this tutorial.

1. JDK 7
2. Maven2

Let's create a java project using maven

1
mvn archetype:generate -DgroupId=com.techiekernel -DartifactId=ParserDemo -Dpackagename=com.techiekernel

Now create a sample xml document to parse

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<products>
 <product>
  <name>R15</name>
  <make>Yamaha</make>
  <engine-cc>150</engine-cc>
  <type>sports</type>
 </product>
 <product>
  <name>Duke</name>
  <make>KTM</make>
  <engine-cc>200</engine-cc>
  <type>Street</type>
 </product>
 <product>
  <name>GS650GS Sertao</name>
  <make>BMW</make>
  <engine-cc>650</engine-cc>
  <type>Enduro</type>
 </product>
 <product>
  <name>Multistada</name>
  <make>Ducati</make>
  <engine-cc>1210</engine-cc>
  <type>Touring</type>
 </product>
</products>

The program to parse the xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.techiekernel.parcer.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 DomReader {
 public static void main(String argv[]) {
   
    try {
  
   File fXmlFile = new File("product.xml");
   
   DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
   
   DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
   
   Document doc = dBuilder.parse(fXmlFile);
   
   doc.getDocumentElement().normalize();
  
   System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
   
   NodeList nList = doc.getElementsByTagName("product");
   
   System.out.println("-----------------------");
  
   for (int temp = 0; temp < nList.getLength(); temp++) {
  
      Node nNode = nList.item(temp);
      if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  
         Element eElement = (Element) nNode;
  
         System.out.println("Name : " + getTagValue("name", eElement));
         System.out.println("Make : " + getTagValue("make", eElement));
            System.out.println("Engine CC : " + getTagValue("engine-cc", eElement));
         System.out.println("Type : " + getTagValue("type", eElement));
  
      }
   }
    } catch (Exception e) {
   e.printStackTrace();
    }
   }
  
   private static String getTagValue(String sTag, Element eElement) {
  NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
  
         Node nValue = (Node) nlList.item(0);
  
  return nValue.getNodeValue();
   }
}

Output:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Root element :products
-----------------------
Name : R15
Make : Yamaha
Engine CC : 150
Type : sports
Name : Duke
Make : KTM
Engine CC : 200
Type : Street
Name : GS650GS Sertao
Make : BMW
Engine CC : 650
Type : Enduro
Name : Multistada
Make : Ducati
Engine CC : 1210
Type : Touring

Source Code:

You can pull the code from GitHub.

0 comments:

Post a Comment