JAXP is an interface for “plugging-in” and using XML processors in Java applications
it includes following packages
org.xml.sax: SAX 2.0 interface
SAX reads the XML document and calls one of your methods for each element or block of text
that it encounters
SAX provides only sequential access to the XML document
SAX is fast and requires very little memory, so it can be used for huge documents
The "Simple API" for XML (SAX) is the event-driven, serial-access
mechanism that does element-by-element processing. The API for this
level reads and writes XML to a data repository or the Web.
org.w3c.dom: DOM Level 2 interface
DOM reads the entire XML document into memory and stores it as a tree data structure
DOM provides “random access” into the XML document
DOM is slow and requires huge amounts of memory, so it cannot be used for large XML
The DOM API is generally an easier API to use. It provides a familiar tree
structure of objects. You can use the DOM API to manipulate the hierarchy
of application objects it encapsulates. The DOM API is ideal for interactive
applications because the entire object model is present in memory, where it
can be accessed and manipulated by the user.
On the other hand, constructing the DOM requires reading the entire XML
structure and holding the object tree in memory, so it is much more CPU
and memory intensive.
structure and holding the object tree in memory, so it is much more CPU
and memory intensive.
Example : 1
Following is the Content of the XML file
Following is the code for reading XML file using SAX
Follwing is the handler class
Follwing is the output
characters: "
"
startElement: employee
characters: "
"
startElement: fname
characters: "Vivek"
Element: /fname
characters: "
"
startElement: lname
characters: "Shah"
Element: /lname
characters: "
"
startElement: email
characters: "reply2vivekshah@gmail.com"
Element: /email
characters: "
"
startElement: mobile
characters: "9725956599"
Element: /mobile
characters: "
"
Element: /employee
characters: "
"
startElement: employee
characters: "
"
startElement: fname
characters: "Deven"
Element: /fname
characters: "
"
startElement: lname
characters: "Sorathiya"
Element: /lname
characters: "
"
startElement: email
characters: "devensorathiya@gmail.com"
Element: /email
characters: "
"
startElement: mobile
characters: "9427944420"
Element: /mobile
characters: "
"
Element: /employee
characters: "
"
Element: /employeeRoot