Java Java JVMandbeyond
Java Java JVMandbeyond
How to create ADF TreeTable program m atically ? Parsing XML in Groov y using Xm lSlurper
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.
//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…
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:
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 ;
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 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;
}
}
}
class Employee{
String id;
0 String firstName;
String lastName;
String location;
@Override
public String toString {
return firstName+" "+lastName+" "+id+" "+location;
}
}
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 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
Yes I agree that the above example calls for the use of JAXB. I took a simpler example to
compare different parsing techniques available.
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
[…] 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...
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