Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


  Tech. Home
  Vehicle Tracking
  Tech. What is?
Jobs At RoseIndia.net!
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Java DOM Tutorial


This tutorial is complete guide to DOM processing.

What is DOM?

Document Object Model: DOM is a platform- and language-neutral interface, that provides a standard model of how the objects in an XML object are put together, and a standard interface for accessing and manipulating these objects and their inter-relationships.

The DOM is an interface that exposes an XML document as a tree structure comprised of nodes. The DOM allows you to programmatically navigate the tree and add, change and delete any of its elements.

The DOM programming interface standards are defined by the World Wide Web Consortium (W3C). The W3C site provides a comprehensive reference of the XML DOM.

  1. Creating Blank DOM Document
    This section shows you how to create the blank DOM document.

  2. Adding Child Elements to the DOM tree
    This lesson shows you how to create root and child elements in the DOM tree.


  3. Getting The XML Root Element
    After reading this section, you will be able to retrieve a  root element from the XML document.  The JAXP (Java APIs for XML Processing) provides a common interface for creating and using xml files using the standard SAX, DOM and XSLTs.
  4. To Count XML Element
    In this section, you will learn to count the elements present in a  XML file using DOM APIs. 
  5. To Count The Elements in a XML File
    In this section, you will learn to count the element in XML document using DOM APIs defined in the org.apache.xerces.parsers.DOMParser  package.
  6. XML Well-Formed-ness
    In this section, you will learn to check the well-formed-ness  of a XML using the DOM interface. A  well-formed  XML  document must follow the xml syntax rules.

  7. Searching an Element in the given XML Document
    In this you will learn to search an element in the specified XML document using DOM APIs defined in the org.apache.xerces.parsers.DOMParser  package.
  8. Create - XML File (Document)
    In this section, you will  learn to create a XML document using the DOM APIs. This XML document uses  1.0 version  and UTF-8 encoding. 

  9. Regenerating XML  file
    In this section, you will learn to get the  elements and its value using DOM APIs. 

  10. XML Error checker and locater (DOM)
    In this section, you will learn to check and locate (line and column number) an error in your XML document using the DOM APIs.  The XML document follows some rules to check its syntax. 
  11. Getting all XML Elements 
    In this section, you will learn to retrieve all elements of the XML file using the DOM APIs. This APIs provides some constructors and methods which helps us to parse the XML file and retrieve all elements.
  12. Adding DOCTYPE to a  XML File
    In this section, you will learn to add a DOCTYPE to your XML file  using the DOM APIs. 
  13. Getting Dom Tree Elements and their Corresponding XML Fragments
    In this section, you will learn to get the elements of a DOM tree and their corresponding XML fragments. Each element of dom tree has a node level starting with '0'. Here the DOM tree elements and their corresponding XML fragments are displayed on the console.
  14. Cloning a XML Element
    In this section, you will learn to create a clone of a  element in the DOM tree. In general, the cloning means to create a duplicate.
  15. Remove Element from XML Document
    In this section, you will learn to remove any element from a given  XML document. Whenever you remove the xml element from the xml document the data are also lost from the xml element.
  16. Getting Data from XML File (Document)
    In this section, you will learn to retrieve the data from a XML file. All xml files store the data. You can add and modify the data in the  xml document using the DOM APIs.
  17. Storing Data (Retrieved from a XML Document) to a File
    In this section, you will learn to store data (retrieved from the XML document) to a specified file (with  extension '.txt', '.doc', '.xls', '.shtml' etc.) in different formats (text, xml, html etc.). 
  18. XML Validate DTD 
    In this section, you will learn to validate a xml file against a  DTD (Document Type Definition)  using the DOM APIs. A DTD defines  the document structure with a list of legal elements and attributes.
  19. Accessing XML file from Java
    In this example we have provided you a simple java example with the source code that will make it possible to access the XML file through Java. For that we have used DOM parser.

XML Tutorials Links
Links of Many XML Tutorials. Here you will find lots of XML Tutorials.



Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

15 comments so far (post your own) View All Comments Latest 10 Comments:


Posted by Thinn Thinn Win on Friday, 09.17.10 @ 16:30pm | #99655

I have to change DOM source of selection code and write new code how can i do this ..can u lz help me

Posted by jaideep on Friday, 08.13.10 @ 22:12pm | #98913

its the best tutorial for all JAXP learners

Posted by suchi on Saturday, 05.22.10 @ 00:32am | #97147

Hi there, thanks for your examples. Can you add examples of both

1) <example>data</example> AND
2) <example fred="bob"></example>

I have spent an hour trying to find out how to do 1) in Java / DOM XML... all examples in the last hour (via google) have only showed examples of how to create 2) :(

Any ideas?

Posted by M on Wednesday, 04.28.10 @ 00:37am | #96659

i want java xml parser source

Posted by gopianth on Monday, 01.4.10 @ 11:12am | #93760

Hi everybody,
where are the packages like org.w3c.dom
org.xml ?

Posted by maq on Monday, 02.9.09 @ 04:48am | #84645


Posted by rafe on Wednesday, 01.28.09 @ 05:14am | #84239

Tutorial is very helpful.
You can have more meaningful method names instead of writing in main methods.

Posted by Sree on Monday, 12.8.08 @ 07:06am | #82510

Dear sir
i want to know how to store XML documents into RDBMS by java.
I am difficult in position about that. So please help me sir.
i want to get java example coding about that sir

please help me

Best regards,

Tin Maung

Posted by Tin Maung on Saturday, 11.15.08 @ 07:49am | #81697

//Create a XML file in java using Parsers
import java.util.ArrayList;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.exolab.castor.xml.*;
import org.apache.xml.serialize.*;
public class MapAction extends HttpServlet {
//Connection con=null;

String path=null;
ArrayList arllist;

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException

// MapForm mf=new MapFrom();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

String first = "3000";
String second="4000";
String caption="This Weeks meals plan";
String palette="5";
String shownames="1";
String showvalues="0";
String useRoundEdges="1";
String legendBorderAlpha="0";
String formatNumbersScale="0";
String numDivLines="11";
String yAxisMaxValue="6000";
String yAxisMinValue="0";
String sun="Sun";
String mon="Mon";
String tue="Tue";
String wed="Wed";
String thu="Thu";
String fri="Fri";
String sat="Sat";
String root="chart";
String categories="categories";
String category="category";
String dataset="dataset";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element rootElement = document.createElement(root);
rootElement.setAttribute("palette", palette);
rootElement.setAttribute("shownames", shownames);
rootElement.setAttribute("showvalues", showvalues);
rootElement.setAttribute("useRoundEdges", useRoundEdges);
rootElement.setAttribute("legendBorderAlpha", legendBorderAlpha);
rootElement.setAttribute("numDivLines", numDivLines);
rootElement.setAttribute("formatNumbersScale", formatNumbersScale);
rootElement.setAttribute("yAxisMaxValue", yAxisMaxValue);
rootElement.setAttribute("yAxisMinValue", yAxisMinValue);

Element categoriesElement = document.createElement(categories);

Element categoryElement = document.createElement(category);
categoryElement.setAttribute("label", sun);

/*categoryElement.setAttribute("label", mon);
categoryElement.setAttribute("label", tue);
categoryElement.setAttribute("label", wed);
categoryElement.setAttribute("label", thu);
categoryElement.setAttribute("label", fri);
categoryElement.setAttribute("label", sat);

String scheduled="ScheduledCalories";
String color="AFD8F8";
Element datasetElement = document.createElement(dataset);
datasetElement.setAttribute("seriesName", scheduled);
datasetElement.setAttribute("showvalues", showvalues);
String sset="set";
String s1="4567";
Element ssetElement = document.createElement(sset);

String current="CurrentCalories";
Element setvalueElement = document.createElement(dataset);
setvalueElement.setAttribute("seriesName", current);
setvalueElement.setAttribute("showvalues", showvalues);
String c1="2500";
Element csetElement = document.createElement(sset);

OutputFormat format = new OutputFormat(document);


//to generate output to console use this serializer

//XMLSerializer serializer = new XMLSerializer(System.out, format);

//to generate a file output use fileoutputstream instead of system.out

XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File("D:\\map.xml")), format);


}catch(Exception ex){



//output XML File like below

<chart caption='This Weeks meals plan' shownames='1' showvalues='0' useRoundEdges='1' legendBorderAlpha='0'
formatNumberScale='0' yAxisMinValue='0' yAxisMaxValue='5000' palette='5'>

<category label='Sunday'/>
<category label='Monday'/>
<category label='Tuesday'/>
<category label='Wednesday'/>
<category label='Thursday'/>
<category label='Friday'/>
<category label='Saturday'/>

<dataset seriesName='Scheduled Calories' color='AFD8F8' showValues='0'>
<set value='3702'/>
<set value='3999'/>
<set value='3172'/>
<set value='2762'/>
<set value='2856'/>
<set value='2846'/>
<set value='3132'/>

<dataset seriesName='Current Calories' color='F6BD0F' showValues='0'>
<set value='2500' />
<set value='2500'/>
<set value='2500' />
<set value='2500' />
<set value='2500' />
<set value='2500' />
<set value='2500' />

Posted by Javed N on Saturday, 10.11.08 @ 11:41am | #81011

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]