Javatpoint Logo
Javatpoint Logo

Java DOM

Document Object Model is a recognition of the World Wide Web Consortium. It makes sense of a connection point that empowers projects to get to and alter the style, design, and items in XML reports. XML parsers that help DOM carry out this point of interaction. It tends to be seen from the picture given underneath as follows.

Parser in Java

There are two kinds of XML parsers to be explicit Simple API for XML and Document Object Model.

  1. DOM(Document object model) parser
  2. SAX(Simple API for XML) parser

What is DOM (Document Object model) Parser?

DOM addresses the Document Object model. The point when an article contains some data about XML reports is called DOM Parser. This seems to be a tree structure. DOM API is carried out by a DOM Parser, which is extremely simple and easy to utilize. It addresses an XML Document into a tree design in which every component addresses tree limbs, makes an In Memory tree portrayal of the XML record, and then parses it. More memory is expected for this.

Highlights of DOM parser

  1. The interior design can be made by DOM Parser.
  2. As a result of these inner designs, the client can get data about the first XML docs.

Benefits of DOM Parser

  1. DOM API is not difficult to utilize, so we can both compose and understand activities.
  2. At the point when a report is required, then it favors a wide part that can be haphazardly gotten to.

Drawbacks of DOM Parser

  1. Its productivity of memory isn't excessively great, and it takes more memory cause XML docs are expected to stack in there.
  2. In contrast with the SAX parser, it is excessively sluggish.

Where to Use DOM

  1. Use it when you know a ton about the construction of a report.
  2. Use it assuming you really want to involve the data in an XML report at least a couple of times.
  3. You want to move portions of an XML report around.

DOM Interfaces

  1. Node: The DOM Node interface is an abstract-based class whereupon numerous other DOM API objects are based, in this manner letting those item types be utilized in much the same way and frequently reciprocally.
  2. Element: It addresses a program component like a package, class, or method.
  3. Attr: it is utilized for addressing a property of a component.
  4. Text: It is the genuine substance of an Element or Attr.
  5. Document: The document addresses the whole XML document.

DOM Methods

S.No Method name Method description
1. getElementId() By using this method, we can access any element practically.It is utilized to, for all intents and purposes, access any component. It gets to the principal component with the predetermined ID.
2. getElementsByClassName() It will likewise return a live HTMLCollection of that multitude of components that have a similar class added to them. In the event that no component is found, it returns a vacant HTMLCollection
3. getElementsByTagName() In the past strategy, we might have had a few blunders. However, this technique can kill those issues. getElementByTagName permits you to look through every one of the components with a predefined label name on your page.
4. querySelector() It returns all of the components that match the particular CSS selectors.
5. querySelectorAll() It will return the primary element that matches the particular group of selectors. Assuming no match is found, 'null' is returned.
6. write("string") This method is used to write the given string in the document.
7. writeln("string") It is similar to the write method, but the only change is the new line character is added at the end.

Example

Example program on Document Object Model:

Html code:

Output:

Java DOM

Program for DOM in Java

To perform DOM in java, we need to follow a few steps:

Step-1: Import the required packages.

Step-2: Creating the DocumentBuilder using DocumentBuilderFactory class and DocumentBuilder class.

Step-3: Creating a Document stream

Step-4: Extract the root component.

Step-5: Analysing the Attributes.

Example

XML code:

Java code:

Output:

Java DOM

Key points of DOM

  1. The abbreviation for DOM is Document Object Model
  2. It stays in a tree structure.
  3. DOM Parser is quicker than SAX Parser.
  4. Best for the more modest size of documents.
  5. It isn't great at making XML documents in low memory.
  6. The inner design can be made by DOM Parser.
  7. It can embed or erase hubs.
  8. In DOM parser in reverse and forward search is conceivable
  9. Reasonable for a huge XML report.
  10. It stacks entire XML reports in memory.

What is SAX (Simple API for XML) Parser?

SAX addresses a Simple API for XML, and a SAX API is executed by SAX Parser. This API was called occasion put together API, which gives interfaces respect to overseers. There are four controller interfaces. ContentHandler, DTDHandler, EntityResolver, and ErrorHandler interface.

It makes no interior construction. Rather, it takes the events of parts of an information report as occasions, and afterward, it lets the client know what it peruses as it peruses the info record. It is reasonable for enormous XML records since it doesn't need stacking the entire XML document.

Highlights of SAX Parser

  1. The inside construction cannot be made by SAX Parser.
  2. These occasion-based SAX parsers work equivalent to the occasion overseer in java.

Benefits of SAX Parser

  1. Extremely easy to utilize and has great productivity of memory.
  2. Its runtime is excessively quick, and it can work for a greater report or record framework.

Drawbacks of SAX Parser

  1. Its capacity to comprehend APIs is excessively under an occasion-based API.
  2. We can't have the foggiest idea about the full data due to a lot of bits of information.

Key points in SAX Parser

  1. The abbrivation of SAX is Simple API for XML.
  2. It's an occasion-based parser.
  3. SAX Parser is slower than DOM Parser.
  4. Best for the bigger sizes of documents.
  5. It is appropriate for making XML documents in java.
  6. The inner design cannot be made by SAX Parser.
  7. It is perused, as it were.
  8. In the SAX parser in reverse route is unimaginable.
  9. Appropriate for effective memory.
  10. A little piece of the XML document is just stacked in memory.

Next TopicJava Exit Code 13





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA