Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What is Document Object in Java DOM?

  • Difficulty Level : Easy
  • Last Updated : 22 Sep, 2021

Document Object Model is a commendation of the World Wide Web Consortium. It explains an interface that enables programs to access and modify the style, structure, and contents of XML documents. XML parsers that support DOM implement this interface. It can be clearly perceived from the image provided below as follows.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

When should one use a DOM parser? 

  1. Use it when you know a lot about the structure of a document.
  2. Use it if you need to use the information in an XML document more than once.
  3. You need to move parts of an XML document around.

What do we get by using DOM?

  • When an XML document is parsed with DOM parser, it gives back a tree structure that contains all the elements of the document.
  • A variety of functions are provided by DOM like you can use it to inspect the contents and structure of the document.

Let us discuss the advantages of DOM as listed below:

  • Used for manipulating document structures.
  • Data persists in memory.
  • You can go forwards and backward in the tree (random access)
  • You can make changes directly to the tree in memory.

DOM Interfaces

  • Node: The DOM Node interface is an abstract base class upon which many other DOM API objects are based, thus letting those object types be used similarly and often interchangeably.
  • Element: It represents a program element such as a package, class, or method.
  • Attr: it is used for representing an attribute of an element.
  • Text: It is the actual content of an Element or Attr.
  • Document: Document represents the entire XML document.

Common DOM methods :

MethodsAction performed
getElementId()It is used to access any element virtually. It accesses the first element with the specified ID.
getElementsByTagName() In the previous method, we may have some errors. But this method can eradicate those issues. getElementByTagName allows you to search all the elements with a specified tag name on your page.

Any element on a page including text & whitespaces of a DOM structure is known as “NODE.” Nodes can be between XHTML Tags.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!