0% found this document useful (0 votes)
28 views

Java Java JVMandbeyond

This document discusses three different parsers for parsing XML in Java: DOM, SAX, and StAX. It provides code examples of parsing an XML document containing employee data using each parser. The DOM parser loads the entire XML document into a tree structure, which is then traversed to extract the data and populate Employee objects. The SAX and StAX parsers parse the XML document sequentially to populate the Employee objects.

Uploaded by

suggestionbox
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Java Java JVMandbeyond

This document discusses three different parsers for parsing XML in Java: DOM, SAX, and StAX. It provides code examples of parsing an XML document containing employee data using each parser. The DOM parser loads the entire XML document into a tree structure, which is then traversed to extract the data and populate Employee objects. The SAX and StAX parsers parse the XML document sequentially to populate the Employee objects.

Uploaded by

suggestionbox
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2/4/2014 Parsing XML using DOM, SAX and StAX Parser in Java | Java, JVM and beyond

Java, JVM and beyond


EX P LORIN G T H E REA LM OF J A V A L A N G UA G E

H OM E JAV A 8 JAV A S C A LA DES IG N BOO KS A LL T H IN G S Search

XML Database XML File XML DTD


Application XML XML version Text to XML

How to create ADF TreeTable program m atically ? Parsing XML in Groov y using Xm lSlurper

Parsing XML using DOM, SAX and StAX


Parser in Java
by M O H A ME D S A N A U L L A on MA Y 2 3 , 2 0 1 3 · 6 C O MMEN T S

Recom m end One person recommends this. Be the first of your friends.

I happen to read through a chapter on XML parsing and building APIs in Java. And I 2
tried out the different parser available on a sample XML. Then I thought of sharing it on
Tw eet
my blog so that I can have a reference to the code as well as a reference for anyone
reading this. In this post I parse the same XML in different parsers to perform the same operation of
populating the XML content into objects and then adding the objects to a list.
RELATED SEARCHES
The sample XML considered in the examples is:
» XML Software
0 <employees>
0 <employee id=" "> » XML Parser
0 <firstName>Rakesh</firstName>
0 <lastName>Mishra</lastName> » XML Integration
0 <location>Bangalore</location>
0 </employee>
» XML Tools
0 <employee id=" "> » Convert XML
0 <firstName>John</firstName>
0 <lastName>Davis</lastName> » Text to XML
0 <location>Chennai</location>
</employee>
<employee id=" ">
<firstName>Rajesh</firstName>
<lastName>Sharma</lastName>
<location>Pune</location>
</employee>
</employees>

And the obejct into which the XML content is to be extracted is defined as below:

0 class Employee{
0 String id;
0 String firstName;
0 String lastName; The Clean Coder
0 String location; Rs.2924
0
0 @Override
0 public String toString {
0 return firstName+" "+lastName+" "+id+" "+location;
0 }
}

There are 3 main parsers for which I have given sample code:
SU BSCR I BE TO B LOG V I A EMA I L

http://blog.sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ 1/7
2/4/2014 Parsing XML using DOM, SAX and StAX Parser in Java | Java, JVM and beyond
DOM Parser Enter your email address to subscribe to this
SAX Parser blog and receive notifications of new posts
StAX Parser by email.

Join 95 other subscribers


Using DOM Parser
I am making use of the DOM parser implementation that comes with the JDK and in my example I Email Address
am using JDK 7. The DOM Parser loads the complete XML content into a Tree structure. And we
SUB SCR IB E
iterate through the Node and NodeList to get the content of the XML. The code for XML parsing
using DOM parser is given below. CO NNEC T TO US …

0 public class DOMParserDemo {


0 Java, JVM and …
0 public static void main String[] args throws Exception {
0 //Get the DOM Builder Factory Follow +1
0 DocumentBuilderFactory factory =
0 DocumentBuilderFactory.newInstance ; + 68
0
0 //Get the DOM Builder
0 DocumentBuilder builder = factory.newDocumentBuilder ;
Find us on Facebook
0
//Load and Parse the XML document
//document contains the complete XML as a Tree. Java, JVM and beyond
Document document = Like
builder.parse
ClassLoader.getSystemResourceAsStream "xml/employee.xml" ;
77 people like Java, JVM and beyond.

List<Employee> empList = new ArrayList<> ;

0 //Iterating through the nodes and extracting the data.


NodeList nodeList = document.getDocumentElement .getChildNodes ;

for int i = 0; i < nodeList.getLength ; i++ { Facebook social plugin

//We have encountered an <employee> tag.


Node node = nodeList.item i ;
SO ME R ECOM MENDA TI O NS…
if node instanceof Element {
Employee emp = new Employee ;
Useful Audio Podcasts for Softw are
emp.id = node.getAttributes . developers
0 getNamedItem "id" .getNodeValue ; 3 people recommend this.

NodeList childNodes = node.getChildNodes ; Using Lam bda Expression to sort a List in


for int j = 0; j < childNodes.getLength ; j++ { Java 8 using Netbeans Lam bda Support
Node cNode = childNodes.item j ; 6 people recommend this.

//Identifying the child tag of employee encountered. Using Notepad++ to Com pile and Run Java
Program s
if cNode instanceof Element {
7 people recommend this.
String content = cNode.getLastChild .
getTextContent .trim ; Shadow ing Variables in Java Dem ystified
0 switch cNode.getNodeName { 2 people recommend this.
case "firstName":
emp.firstName = content;
break;
case "lastName":
emp.lastName = content;
Facebook social plugin
break;
case "location":
emp.location = content; CA TEGO RI ES
break;
0 } 2008 Season 2 0 1 0 Sea son Books
}
} Code Design Events Formula 1
}
empList.add emp ; General google Groov y HTML5
}
Java Ja v a FX NetBeans IDE NITK Open
Source Scala Sports Sun CA
//Printing the Employee list populated. Ubuntu Unix
for Employee emp : empList {
0 System.out.println emp ;
} RE CENT LY A DDED…

} Another sample for using JAXB November


} 24, 2013

class Employee{ Book Review: Peopleware October 25,


String id; 2013
String firstName;

http://blog.sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ 2/7
2/4/2014 Parsing XML using DOM, SAX and StAX Parser in Java | Java, JVM and beyond
String lastName; JSON Processing support in JavaEE 7 and
0 String location;
JSR-353 October 18, 2013
@Override [Promo]Explore something new this
public String toString {
return firstName+" "+lastName+" "+id+" "+location; Columbus Day with Packt’s biggest ever
} sale October 16, 2013
} Creating Websockets in JavaEE 7 October
8, 2013
The output for the above will be:

Rakesh Mishra Bangalore A R CHI V ES


John Davis Chennai
Rajesh Sharma Pune Select Month

Using SAX Parser DI SCLA I MER


SAX Parser is different from the DOM Parser where SAX parser doesn’t load the complete XML into
Some of the links contained within this site
the memory, instead it parses the XML line by line triggering different events as and when it
have my referral id, which provides me with
encounters different elements like: opening tag, closing tag, character data, comments and so on.
a small commission for each sale. Thank
This is the reason why SAX Parser is called an event based parser. you for your support.

Along with the XML source file, we also register a handler which extends the DefaultHandler class.
The DefaultHandler class provides different callbacks out of which we would be interested in:

startElement() – triggers this event when the start of the tag is encountered.
endElement() – triggers this event when the end of the tag is encountered.
characters() – triggers this event when it encounters some text data.

The code for parsing the XML using SAX Parser is given below:

0 import java.util.ArrayList;
0 import java.util.List;
0 import javax.xml.parsers.SAXParser;
0 import javax.xml.parsers.SAXParserFactory;
0 import org.xml.sax.Attributes;
0 import org.xml.sax.SAXException;
0 import org.xml.sax.helpers.DefaultHandler;
0
0 public class SAXParserDemo {
0
public static void main String[] args throws Exception {
SAXParserFactory parserFactor = SAXParserFactory.newInstance ;
SAXParser parser = parserFactor.newSAXParser ;
SAXHandler handler = new SAXHandler ;
parser.parse ClassLoader.getSystemResourceAsStream "xml/employee.xml" ,
handler ;

//Printing the list of employees obtained from XML


for Employee emp : handler.empList {
0 System.out.println emp ;
}
}
}
/**
* The Handler for SAX Events.
*/
class SAXHandler extends DefaultHandler {

List<Employee> empList = new ArrayList<> ;


0 Employee emp = null;
String content = null;
@Override
//Triggered when the start of tag is found.
public void startElement String uri, String localName,
String qName, Attributes attributes
throws SAXException {

switch qName {
//Create a new Employee object when the start tag is found
0 case "employee":
emp = new Employee ;
emp.id = attributes.getValue "id" ;
break;
}
}

http://blog.sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ 3/7
2/4/2014 Parsing XML using DOM, SAX and StAX Parser in Java | Java, JVM and beyond
@Override
public void endElement String uri, String localName,
String qName throws SAXException {
0 switch qName {
//Add the employee to list once end tag is found
case "employee":
empList.add emp ;
break;
//For all other end tags the employee has to be updated.
case "firstName":
emp.firstName = content;
break;
case "lastName":
0 emp.lastName = content;
break;
case "location":
emp.location = content;
break;
}
}

@Override
public void characters char[] ch, int start, int length
0 throws SAXException {
content = String.copyValueOf ch, start, length .trim ;
}

class Employee {

String id;
String firstName;
0 String lastName;
String location;

@Override
public String toString {
return firstName + " " + lastName + " " + id + " " + location;
}
}

The output for the above would be:

Rakesh Mishra Bangalore


John Davis Chennai
Rajesh Sharma Pune

Using StAX Parser


StAX stands for Streaming API for XML and StAX Parser is different from DOM in the same way
SAX Parser is. StAX parser is also in a subtle way different from SAX parser.

The SAX Parser pushes the data but StAX parser pulls the required data from the XML.
The StAX parser maintains a cursor at the current position in the document allows to extract the
content available at the cursor whereas SAX parser issues events as and when certain data is
encountered.

XMLInputFactory and XMLStreamReader are the two class which can be used to load an XML file.
And as we read through the XML file using XMLStreamReader, events are generated in the form of
integer values and these are then compared with the constants in XMLStreamConstants. The below
code shows how to parse XML using StAX parser:

0 import java.util.ArrayList;
0 import java.util.List;
0 import javax.xml.stream.XMLInputFactory;
0 import javax.xml.stream.XMLStreamConstants;
0 import javax.xml.stream.XMLStreamException;
0 import javax.xml.stream.XMLStreamReader;
0
0 public class StaxParserDemo {
0 public static void main String[] args throws XMLStreamException {
0 List<Employee> empList = null;
Employee currEmp = null;
String tagContent = null;
XMLInputFactory factory = XMLInputFactory.newInstance ;
XMLStreamReader reader =
factory.createXMLStreamReader
http://blog.sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ 4/7
2/4/2014 Parsing XML using DOM, SAX and StAX Parser in Java | Java, JVM and beyond
ClassLoader.getSystemResourceAsStream "xml/employee.xml" ;

while reader.hasNext {
int event = reader.next ;
0
switch event {
case XMLStreamConstants.START_ELEMENT:
if "employee".equals reader.getLocalName {
currEmp = new Employee ;
currEmp.id = reader.getAttributeValue 0 ;
}

if "employees".equals reader.getLocalName {
empList = new ArrayList<> ;
}
0 break;

case XMLStreamConstants.CHARACTERS:
tagContent = reader.getText .trim ;
break;

case XMLStreamConstants.END_ELEMENT:
switch reader.getLocalName {
case "employee":
empList.add currEmp ;
0 break;
case "firstName":
currEmp.firstName = tagContent;
break;
case "lastName":
currEmp.lastName = tagContent;
break;
case "location":
currEmp.location = tagContent;
break;
0 }
break;

case XMLStreamConstants.START_DOCUMENT:
empList = new ArrayList<> ;
break;
}

0 //Print the employee list populated from XML


for Employee emp : empList {
System.out.println emp ;
}

}
}

class Employee{
String id;
0 String firstName;
String lastName;
String location;

@Override
public String toString {
return firstName+" "+lastName+" "+id+" "+location;
}
}

The output for the above is:

Rakesh Mishra Bangalore


John Davis Chennai
Rajesh Sharma Pune

With this I have covered parsing the same XML document and performing the same task of
populating the list of Employee objects using all the three parsers namely:

DOM Parser
SAX Parser
StAX Parser

Recom m end One person recommends this. Be the first of your friends.

http://blog.sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ 5/7
2/4/2014 Parsing XML using DOM, SAX and StAX Parser in Java | Java, JVM and beyond
Related posts:

1. How to create ADF TreeTable programmatically? Tweet Using ADF-BC to create ADF TreeTable
is simple and...
2. Predicate and Consumer Interface in java.util.function package in Java 8 Tweet In my previous
post I wrote about Function interface...

Related posts brought to you by Y et Another Related Posts Plugin.

Tagge d with: dom • sax • stax • XML

Related Searches
› XML Programming › Application XML
› XML Integration › XML Tutorials
› XML Software › Convert XML

A BO UT T H E A UT H OR

Mohamed Sanaulla
V isit Authors Website →

6 Responses to parsing xml using dom, sax and stax parser in java
Parsing XML in Groovy using XmlSlurper | Java, JVM and beyond says:
May 2 5, 2 01 3 at 1 2 :1 5 AM

[...] Parsing XML using DOM, SAX and StAX Parser in Java [...]

R EP L Y

Creating External DSLs using ANT LR and Java | Java, JVM and beyond says:
July 1 7 , 2 01 3 at 4 :2 2 PM

[...] ANTLR then makes use of this grammar to generate the Parser, Lexer and a Listener
which are nothing but java classes extending/implementing some classes from the ANTLR
library. The creators of the DSL must make use of these java classes to load the external
DSL, parse it and then using the listener populate the semantic model as and when the
parser encounters certain nodes (think of this as a variant of SAX parser for XML) [...]

R EP L Y

lukaseder says:
July 2 2 , 2 01 3 at 1 :06 PM

If you have a simple bean like the one you’ve shown, why not just use JAXB? All you have
to do is add some annotations to your bean and run a little unmarshal call

R EP L Y

Mohamed Sanaulla says:


July 2 2 , 2 01 3 at 1 :3 5 PM

Yes I agree that the above example calls for the use of JAXB. I took a simpler example to
compare different parsing techniques available.

Will JAXB not require a XSD to be defined?

R EP L Y

lukaseder says:
July 2 2 , 2 01 3 at 3 :06 PM

I don’t think you need an XSD with JAXB. People tend to use XSDs to define the XML
formats and then generate annotated Java beans using XJC, though. XSDs are
probably simpler to write than tons of correctly annotated POJOs
http://blog.sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ 6/7
2/4/2014 Parsing XML using DOM, SAX and StAX Parser in Java | Java, JVM and beyond
R EP L Y

JSON Processing in JavaEE 7- Using Streaming API says:


Nov em ber 2 4 , 2 01 3 at 9 :2 0 AM

[…] in this post I will cover parsing JSON using Streaming API. The Streaming API is
exactly similar to StAX parser for XML. The JSON parser goes through each line of the
JSON data and generates different events depending […]

R EP L Y

Leave a Reply
Enter your comment here...

A FF ILIA T ION S T OP POS T S & PA G ES C A T EG ORI ES REC EN T C OMMEN T S C A T EG ORI ES

Using Notepad++ to 2008 Season 2 0 1 0 Sea son Mohamed Sanaulla on How Books
Compile and Run Java Books Code Design coding to Interface and Code
Programs Inheritance helped me Design
Events Formula 1
Using JAXB to generate
XML from the Java, XSD
General google Groov y reuse code
Javed on How coding to
Events
Formula 1
Parsing XML using DOM, HTML5 Java Ja v a FX Interface and Inheritance 2008 Season
SAX and StAX Parser in NetBeans IDE NITK Open helped me reuse code 2010 Season
Java Source Scala Sports Javed on How coding to General
Null, null, Nil, Nothing, Sun CA Ubuntu Unix Interface and Inheritance google
None, and Unit in Scala helped me reuse code Groovy
Arrays.sort versus Manna on SOLID- Open HTML5
Arrays.parallelSort Closed Principle Java
Cohesion and Coupling: krishna on Another sample JavaFX
Two OO Design Principles for using JAXB NITK
Using JAXB to generate Playing with Collections Open Source
Java Objects from XML API in Scala on Partially NetBeans IDE
document applied functions in Scala Scala
Using Lambda Expression Koong Jumnian Hatta on Sports
to sort a List in Java 8 using Using Notepad++ to Sun CA
Netbeans Lambda Support Compile and Run Java Unix
Execute external process Programs Ubuntu
from within JVM using
Apache Commons Exec
library
Static Members: Static
Methods and Static
Variables

http://blog.sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ 7/7

You might also like