We Can Insert or Delete Nodes We Can't Insert or Delete A Node
We Can Insert or Delete Nodes We Can't Insert or Delete A Node
We Can Insert or Delete Nodes We Can't Insert or Delete A Node
DOM SAX
Tree data structure Event based model
Random access Serial access
High memory usage Low memory usage
Used to edit the document Used to process parts of the document
Used to process multiple times (document is Used to process the document only once
loaded in memory)
Stores the entire XML document into memory Parses node by node
before processing
Occupies more memory Doesn’t store the XML in memory
We can insert or delete nodes We can’t insert or delete a node
Traverse in any direction. Top to bottom traversing
Document Object Model (DOM) API SAX is a Simple API for XML
import javax.xml.parsers.*; Packages required to import
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.*;
import import org.xml.sax.*;
javax.xml.parsers.DocumentBuilderFactory;
DOM is slow rather than SAX SAX generally runs a little faster than DOM
Document Object Model is for defining the standard for accessing and manipulating XML documents.
XML DOM is used for
• Loading the xml document
• Accessing the xml document
• Deleting the elements of xml document
• Changing the elements of xml document
According to the DOM, everything in an XML document is a node. It considers
• The entire document is a document node
• Every XML element is an element node
• The text in the XML elements is text nodes
• Every attribute is an attribute node
• Comments are comment nodes
The following DOM java Classes are necessary to process the XML document:
• DocumentBuilderFactory class creates the instance of DocumentBuilder.
• DocumentBuilder produces a Document (a DOM) that conforms to the DOM
specification
The following methods and properties are necessary to process the XML document:
Property Meaning
nodeName Finding the name of the node
nodeValue Obtaining value of the node
parentNode To get parnet node
childNodes Obtain child nodes
attributes For getting the attributes values
Method Meaning
getElementByTagName(name) To access the element by specifying its name
appendChild(node) To insert a child node
removeChild(node) To remove existing child node
Text t1=doc.createTextNode("501");
Text t2=doc.createTextNode("naveen");
Text t3=doc.createTextNode("90");
idele.appendChild(t1);
nameele.appendChild(t2);
markesele.appendChild(t3);
studentele.appendChild(idele);
studentele.appendChild(nameele);
studentele.appendChild(markesele);
rootele.appendChild(studentele);
doc.appendChild(rootele);
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.
Let’s create a demo program to read xml file with SAX parser to understand fully.
Student.xml
<?xml version="1.0" encoding="UTF-8" ?>
- <students-details>
- <student>
<studentid>501</studentid>
<name>Ramu</name>
<address>ECIL</address>
<gender>Male</gender>
</student>
- <student>
<studentid>502</studentid>
<name>Mahi</name>
<address>BHEL</address>
<gender>Male</gender>
</student>
</students-details>