Java XML Interview Questions and Answers For Freshers Part-2
11.What are the advantages of DOM Parsing?
The DOM is a common interface for manipulating document structures. One of its design goals is that Java code written for one DOM-compliant parser should run on any other DOM-compliant parser without changes.
12.What are the key components/interfaces of DOM Parsing?
The DOM defines several Java interfaces. Here are the most common interfaces −
Node – The base datatype of the DOM.
Element – The vast majority of the objects you’ll deal with are Elements.
Attr Represents an attribute of an element.
Text The actual content of an Element or Attr.
Document Represents the entire XML document. A Document object is often referred to as a DOM tree.
13.Name some of the important DOM parsing methods?
When you are working with the DOM, there are several methods you’ll use often −
Document.getDocumentElement() – Returns the root element of the document.
Node.getFirstChild() – Returns the first child of a given Node.
Node.getLastChild() – Returns the last child of a given Node.
Node.getNextSibling() – These methods return the next sibling of a given Node.
Node.getPreviousSibling() – These methods return the previous sibling of a given Node.
Node.getAttribute(attrName) – For a given Node, returns the attribute with the requested name.
14.Can we create an XML document using DOM parser?
Yes! Using DOM parser, we can parse, modify or create a XML document.
15.What SAX stands for?
SAX stands for Simple API for XML.
16.What is a SAX Parser?
SAX Parser is an event-based parser for xml documents.
17.How a SAX Parser works?
SAX (the Simple API for XML) is an event-based parser for xml documents.Unlike a DOM parser, a SAX parser creates no parse tree. SAX is a streaming interface for XML, which means that applications using SAX receive event notifications about the XML document being processed an element, and attribute, at a time in sequential order starting at the top of the document, and ending with the closing of the ROOT element.
18.When to use a SAX Parser?
You should use a SAX parser when −
You can process the XML document in a linear fashion from the top down
The document is not deeply nested
You are processing a very large XML document whose DOM tree would consume too much memory.Typical DOM implementations use ten bytes of memory to represent one byte of XML
The problem to be solved involves only part of the XML document
Data is available as soon as it is seen by the parser, so SAX works well for an XML document that arrives over a stream
19.What are the disadvantages of SAX Parsing?
We have no random access to an XML document since it is processed in a forward-only manner
If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own
20.Can we create an XML document using SAX parser?
No! Using SAX parser, we can only parse or modify a XML document.