Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: What is the difference between LSParser and DocumentBuilder

I can't find any information on org.w3c.dom.ls.LSParser. I know it is an interface but there is an on only way to get a concrete object afaik.

DOMImplementationLS factory = (DOMImplementationLS) myXMLDocument.getImplementation();
LSParser parser = factory.createLSParser(DOMImplementationLS.MODE_ASYNCHRONOUS, null);

How is LSParser different from javax.xml.parsers.DocumentBuilder (or SAXParser)

like image 845
barsan-md Avatar asked Jun 21 '16 22:06

barsan-md


People also ask

What is the use of DocumentBuilder in Java?

Class DocumentBuilder. Defines the API to obtain DOM Document instances from an XML document. Using this class, an application programmer can obtain a Document from XML. An instance of this class can be obtained from the DocumentBuilderFactory.

What is setNamespaceAware?

The Javax.xml.parsers.DocumentBuilderFactory.setNamespaceAware(boolean awareness) method specifies that the parsers created by this factory will provide support for XML namespaces. By default the value of this is set to false.

What is setExpandEntityReferences?

setExpandEntityReferences(boolean expandEntityRef) Specifies that the parser produced by this code will expand entity reference nodes. abstract void. setFeature(String name, boolean value) Set a feature for this DocumentBuilderFactory and DocumentBuilder s created by this factory.


1 Answers

First, a SAXParser is different from the javax.xml.parsers.DocumentBuilder and LSParser, in that it streams through the XML, instead of building a Document Object Model (DOM).

That leaves us with explaining javax.xml.parsers.DocumentBuilder and LSParser. The DOM spec has levels to define the structure and behavior of the DOM. There are three levels. (1, 2, and 3). LSParser stands for "Load and Save Parser". This Parser provides support for DOM Level 3. It implements the behavior defined here - https://www.w3.org/TR/DOM-Level-3-LS/load-save.html . Since LSParser supports the DOM Level 3 standard, it allows event handling (when DOM is loaded), saving and filtering. Note that this is a spec, so it can be implemented in any language.

In contrast, javax.xml.parsers.DocumentBuilder is a Java based API (JAXP). It is a project, not a spec.

LSParser spec has been influenced by both the JAXP and SAX projects. It just standardized the parsing of XML and using the DOM.

References: https://www.amazon.com/Processing-XML-documents-Oracle-JDeveloper/dp/1847196667 (Refer Chapter 7 - relevant to this topic).

https://www.w3.org/TR/DOM-Level-3-LS/load-save.html (this is the full spec).

like image 165
Shankar P S Avatar answered Jan 02 '23 13:01

Shankar P S