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.
- DOM(Document object model) parser
- 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
- The interior design can be made by DOM Parser.
- As a result of these inner designs, the client can get data about the first XML docs.
Benefits of DOM Parser
- DOM API is not difficult to utilize, so we can both compose and understand activities.
- At the point when a report is required, then it favors a wide part that can be haphazardly gotten to.
Drawbacks of DOM Parser
- Its productivity of memory isn't excessively great, and it takes more memory cause XML docs are expected to stack in there.
- In contrast with the SAX parser, it is excessively sluggish.
Where to Use DOM
- Use it when you know a ton about the construction of a report.
- Use it assuming you really want to involve the data in an XML report at least a couple of times.
- You want to move portions of an XML report around.
- 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.
- Element: It addresses a program component like a package, class, or method.
- Attr: it is utilized for addressing a property of a component.
- Text: It is the genuine substance of an Element or Attr.
- Document: The document addresses the whole XML document.
||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.
||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
||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.
||It returns all of the components that match the particular CSS selectors.
||It will return the primary element that matches the particular group of selectors. Assuming no match is found, 'null' is returned.
||This method is used to write the given string in the document.
||It is similar to the write method, but the only change is the new line character is added at the end.
Example program on Document Object Model:
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.
Key points of DOM
- The abbreviation for DOM is Document Object Model
- It stays in a tree structure.
- DOM Parser is quicker than SAX Parser.
- Best for the more modest size of documents.
- It isn't great at making XML documents in low memory.
- The inner design can be made by DOM Parser.
- It can embed or erase hubs.
- In DOM parser in reverse and forward search is conceivable
- Reasonable for a huge XML report.
- 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
- The inside construction cannot be made by SAX Parser.
- These occasion-based SAX parsers work equivalent to the occasion overseer in java.
Benefits of SAX Parser
- Extremely easy to utilize and has great productivity of memory.
- Its runtime is excessively quick, and it can work for a greater report or record framework.
Drawbacks of SAX Parser
- Its capacity to comprehend APIs is excessively under an occasion-based API.
- We can't have the foggiest idea about the full data due to a lot of bits of information.
Key points in SAX Parser
- The abbrivation of SAX is Simple API for XML.
- It's an occasion-based parser.
- SAX Parser is slower than DOM Parser.
- Best for the bigger sizes of documents.
- It is appropriate for making XML documents in java.
- The inner design cannot be made by SAX Parser.
- It is perused, as it were.
- In the SAX parser in reverse route is unimaginable.
- Appropriate for effective memory.
- A little piece of the XML document is just stacked in memory.