Dom is an official recommendation of the World Wide Web Consortium (W3C). Java Dom parser api for xml is intended for working with XML as an object graph (a tree like structure) in memory – so called "Document Object Model (DOM)".
Dom parser traverses the input XML file and creates DOM objects corresponding to the nodes in XML file. These DOM objects are linked together in a tree like structure. Once the parser is done with parsing process, you get this tree-like DOM object structure back from it. Now you can traverse the DOM structure back and forth as you want to get/update/delete data from it.
Main advantage of using Dom parser are given below:
Main disadvantage of using Dom parser are given below:
If we have want to traverse in any direction or/and add/delete a node run time then we should go for Dom parser.
Here is some common interfaces used by Dom
Here are few commonly used methods of Dom:
In this example we will parse book.xml using DOM parser and populate object of corresponding class with parsed data. The xml which we are going to parse is given below:
I made 2 class to hold parsed data of book.xml are given below:
I wrote BookDomParser.java which is responsible for parsing and populating object of BookCatalog class. My dom parser code is given below:
I kept 2 constructor in my Dom parser class. Either pass full path of file or Object of InputStream to create an instance of BookDomParser class.
Here is Main class from where I am intiating dom parsing.