XML 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 are text nodes Every
attribute is an attribute node Comments are comment nodes
DOM
Levels
Level 1
Core: W3C Recommendation, October 1998
o It has
feature for primitive navigation and manipulation of XML trees o other Level 1
features are: All HTML features
Level 2
Core: W3C Recommendation, November 2000
o It adds
Namespace support and minor new features
o other
Level 2 features are: Events, Views, Style, Traversal and Range Level 3 Core:
W3C Working Draft, April 2002
o It supports: Schemas, XPath, XSL, XSLT We can
access and parse the XML document in two ways:
o Parsing
using DOM (tree based) o Parsing using SAX (Event based)
Parsing
the XML doc. using DOM methods and properties are called as tree based approach
whereas using SAX (Simple Api for Xml) methods and properties are called as
event based approach.
1. DOM based XML Parsing:(tree
based)
JAXP is a
tool, stands for Java Api for Xml Processing, used for accessing and
manipulating xml document in a tree based manner.
In this
approach, to access XML document, the document object model implementation is
defined in the following packages:
javax.xml.parsers
org.w3c.dom
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:
To Count
the Elements in a XML File
This
program takes a file name from the console and checks its availability. If the
file exists then a parser is created using Document Builder. This object parses
the given XML document. It searches the specified element name and counts its
occurrence in the xml file. If the given element doesn't exist it displays the
'0' element.
Step 1: Create an
xml file: Employee-Detail.xml <?xml version = "1.0"
?>
<Employee-Detail>
<Employee>
<Emp_Id>
E-001 </Emp_Id>
<Emp_Name>
Vinod </Emp_Name> <Emp_E-mail> Vinod@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-002 </Emp_Id> <Emp_Name> Arun
</Emp_Name>
<Emp_E-mail>
Arun@yahoo.com </Emp_E-mail> </Employee>
</Employee-Detail>
Step 2:
Create a java based dom for counting the number of elements in xml file. import
org.w3c.dom.*;
import
javax.xml.parsers.*; import java.io.*;
public
class CountNodes
{
public
static void main(String[] args)
{
try
{
File file
= new File(args[0]); // Employee_Detail.xml is a file to be parsed is given as
//input from command prompt
DocumentBuilderFactory
factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder =
factory.newDocumentBuilder(); // parser is created Document doc = builder.parse(file);
// file is parsed
NodeList
list = doc.getElementsByTagName(“Emp_Id”); // the element to be searched is
specified
System.out.println("Number
of nodes: " + list.getLength()); // counting no. of occurrences }catch
(Exception e){ }
}
}
Output:
Number of
nodes: 2
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.