Document Object Model (DOM)
The DOM is the W3C (WWW Consortium) standard.
DOM represents a XML document as a tree of nodes.
The Document Object Model (DOM) is an application programming interface (API) which maps out an entire page as a document composed of a hierarchy of nodes. Each part of an HTML or XML page is a derivative of a node.
In java org.w3c.dom, javax.xml packages provide necessary classes and interface to manipulate XML file.
By using DOM to retrieve XML nodes we have to do following steps
1) Create the object for javax.xml.parsers.DocumentBuilderFactory . It is the abstract class. Use static newInstance() of the DocumentBuilderFactory class to create an object for DocumentBuilderFactory.
2) After creating DocumentBuilderFactory, then you can create DocumentBuilder class object. It is also a abstract class. The object of DocumentBuilderFactory can be used to create DocumentBuilder class object. Use DocumentBuiler newDocumentBuilder() of DocumentBuilderFactory to create DocumentBuilder class object.
3) The final aim is that creating Document class object. Use Document parse(File) in DocumentBuilder class to create the object for Document class.
4) The Document interface is the sub interface of Node interface.
Object creation for Document interface
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
File file=new File("xmlfile1.xml");
Document doc=builder.parse(file);
Retrieving context node from xmlfile
Element root=doc.getDocumentElement();
System.out.println(root.getNodeName());
Creating NodeList object to retrieve the child nodes of context node
NodeList nodelist=root.getChildNodes();
Above nodelist refers the elements under context node.\
Consider following xmlfile.
xmlfile1.xml
<?xml version="1.0"?>
<!--XMLTextWriter Example-->
<products>
<product>
<type>Keyboard</type>
<price>600.00</price>
<company>TVS</company>
</product>
<item>
<type>Monitor</type>
<price>6000.00</price>
<company>Samsung</company>
</item>
</products>
Under <products> context node, there are two elements found.
Now NodeList object refers two elements.
How to know the count of children are referred by NodeList? Use int getLength() of NodeList
interface.
Node Interface
The basic unit of DOM is the node. Every thing in an XML document (individual elements, attributes in a start tag, comments, element text, and the document as the whole) is a node.
org.w3c.dom.Node Interface Important methods
NamedNodeMap getAttributes()
if the node is an element Returns the attributes of this node, otherwise returns null.
NodeList getChildNodes()
Returns the list of all child nodes.
Node getFirstChild()
Returns the first child node or null, if this node isn’t an Element.
Node getLastChild()
Returns the last child node or null, if this node isn’t an Element.
Node getNextSibling()
Returns the next child of the parent or null if parent node is not an Element.
String getNodeName()
Returns the name of node for following node types.
Element
Attribute
Entity
For unnamed types returning values listed below
Text - #text
CDataSection - #cdata-section
Comment - #comment
int getNodeType()
Returns node type as integer
Node interface declares the constants to indicate node types.
String getNodeValue()
For attributes and text type nodes returns the text, otherwise null.
String getTextContent()
Returns text value associated with the current node.
XMLParserDemo2.java
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
class XMLParserDemo2
{
public static void main(String args[])
{
try
{
/* *********** Creating Document object ****************** */
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
File file=new File("xmlfile1.xml");
Document doc=builder.parse(file);
/* *********************************** ****************** */
/* ****** Print context node name by using Element interface object **** */
Element root=doc.getDocumentElement();
System.out.println(root.getNodeName());
/* *********************************** ****************** */
System.out.println();
/* ****** Retrieve the child nodes under context node as NodeList object ** */
NodeList nodelist=root.getChildNodes();
/* *********************************** ****************** */
/* ****** Retrieve the child nodes referred by NodeList object ** */
for (int i=0;i<nodelist.getLength();i++)
{
/* ****** Retrieve the first child node of NodeList object ** */
Node n=nodelist.item(i);
if (n.getNodeType()==Node.ELEMENT_NODE)
{
System.out.println(n.getNodeName());
/* ****** Retrieve the child nodes referred by n. n is Node object ** */
NodeList nodelistchild=n.getChildNodes();
/* ****** to print the tag and values from xml file.** */
for (int j=0;j<nodelistchild.getLength();j++)
{
Node m=nodelistchild.item(j);
if (m.getNodeType()==Node.ELEMENT_NODE)
{
System.out.print(m.getNodeName()+"="); // Prints tag name
System.out.println(m.getTextContent()); // Prints value of tag.
}//inner if
}//inner for
System.out.println();
}//outer if
}//outer for
}//try
catch(Exception e)
{
e.printStackTrace();
}
}
}