VisualBuilder
  Home > Java > Tutorials > Basics of programming using DOM - Java XML Tutorial
Tell a friend
Link to us
Total Members
      Members: 84663
     
Sitemap Forum Chat
Home
Java XML Tutorial Home
1 . What is XML?
2 . HTML vs. XML
3 . XML Basics
4 . Why use XML in Java?
5 . Application areas of XML
6 . Standards
7 . Setting up the environment for XML and Java
8 . JAVA XML API
9 . Simple API for XML (SAX)
10 . Document Object Model (DOM)
11 . Basics of programming using DOM
12 . Should I use DOM or SAX? 
 
 
Java Home
Java Members (27610)
Java Member Articles ( 40 )
Java Discussion (273)
Java Q & A ( 153 )
- Java Ask Question
- Java Questions
- Java Unanswered Questions
Java Resources
Java Source Code (1095)
Java Articles (550)
Java Blogs (118)
Java Jobs (797)
Java Components (84)
Java Books (169)
Java Websites (126)
Java News (103)
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.Net Security,Internationalisation And Deployment
ASP.NET Tutorial
C# Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (451 )
Source Code (3275 )
Articles (359 )
Books (372 )
Components (1596 )
News (892 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Java xml Tutorial
 Basics of programming using DOM
  << Prev: Document Object Model (DOM) Next: Should I use DOM or SAX?  >>

After you have made sure that your environment has been set up correctly (see Setting up the environment for XML and Java section), you may write your first Java and XML example.
For this example we will use the DOM API discussed in the previous section.

This is a simple example that will read the text “Hello World” from a xml file called “hello.xml”.


1) Import package org.w3c.dom


The Java interfaces have been defined by W3C and are contained in the package org.w3c.dom.



import org.w3c.dom.*;


2) Import Vendor dependent Parser.


The next step is to import a vendor dependent XML parser.
In our case it will be the xerces DOM parser that we configured. 

import org.apache.xerces.parsers.DOMParser; 


3) On calling DOMHelloWorld


The main method of DOMHelloWorld.java will check that the filename
of the xml file has been provided as an argument.



public static void main(String[] args) 

if (args.length != 1) 

System.out.println("usage: java DOMHelloWorld hello.xml"); 
System.exit(0); 


String xmlfilename = args[0]; 


} 


4) Parsing the XML document 


First we must create an instance of the parser (vendor specific parser).
This is the same parser we imported earlier in step 2.



DOMParser xmlparser = new DOMParser(); 


5) Parse the xml file.


This is really easy because the parser does it for you.
All you have to do is call the parse method with the name of the xml file. 



xmlparser.parse(xmlfilename); 


If you look at the API documentation that comes with the Xerces parser
and search for the parse method you will notice something special. 
DOMParser and SAXParser are subclasses of XMLParser.
The parse method throws two exceptions, SAXException and 
java.io.IOException. 

If you try to compile the source code without catching the exception, an error will occur (java.io.IOException must be caught).


Under the previous import statements add the imports
for the IOException and SAXException classes .


import java.io.IOException; 
import org.xml.sax.SAXException;
 

So now we put a try/catch block around the parse method of DOMParser.

try 

xmlparser.parse(xmlfilename); 

catch (IOException e) 

System.out.println("Error reading xml file: " e.getMessage()); 

catch (SAXException e) 

System.out.println("Error in parsing: " e.getMessage()); 
}
 


6) Accessing the DOM tree 


As DOM creates a tree-based structure based on the xml file,
we will need to access the information stored in the tree.
To access the tree call getDocument() and this returns a Document.



Document doc = xmlparser.getDocument(); 



This Document represents the entire XML document. The data we need to access is stored in nodes. These nodes can have child nodes. Therefore what you need to do next is walk through the Tree structure (Document) and display the data stored in the Nodes.


Now the Fun starts!!! 


7) Walking the nodes


Next we will write a method to display the data in a node. The method will be called displayNode and will take one parameter, the start node. 


This method will start from the first node and walk through all the nodes in the XML using recursion.


The nodes in the XML document are of different node types. The node types can be divided into two broad categories; structural nodes and content nodes.
Structural nodes are not actually part of the content in the document but are used to provide syntax structure.


The following is a list of the different types of nodes:














Node Type Category

ATTRIBUTE_NODE

CDATA_SECTION_NODE

COMMENT_NODE

DOCUMENT_FRAGMENT_NODE

DOCUMENT_NODE

DOCUMENT_TYPE_NODE

ELEMENT_NODE

ENTITY_NODE

ENTITY_REFERENCE_NODE

NOTATION_NODE

PROCESSING_INSTRUCTION_NODE

TEXT_NODE 



Content

Content

Content

Content

Content

Structural

Content

Structural

Content

Structural

Structural

Content

Content


 





The main nodes we are interested in are DOCUMENT_NODE, ELEMENT_NODE and ATTRIBUTE_NODE.


(to be completed!)


  << Prev: Document Object Model (DOM) Next: Should I use DOM or SAX?  >>
Java Xml Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising