JDOM represents an XML document as an instance of the
Builders
You can build documents from any data source using builder classes found in the
Builders are also being developed that construct JDOM
Examples with SAX builder:
1. String source
2.From URL
Validation
The
The defaults are to use Apache's open source Xerces parser and to turn off validation. Notice that the
org.jdom.Document
class. The Document
class is a lightweight class that can hold a DocType
, multiple ProcessingInstruction
objects, a root Element
, and Comment
objects. You can construct a Document
from scratch without needing a factory:Document doc = new Document(new Element("rootElement"));
But in case of JDOM there are multiple ways of building Document. Egs. :
Builders
SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(url);
You can build documents from any data source using builder classes found in the
org.jdom.input
package. Currently there are two builders, SAXBuilder
and DOMBuilder
. SAXBuilder
uses a SAX parser behind the scenes to build the Document
from the file; the SAXBuilder
listens for the SAX events and builds a corresponding Document
in memory. That approach is very fast (basically as fast as SAX), and it is the approach we recommend. DOMBuilder
is another alternative that builds a JDOM Document
from an existing org.w3c.dom.Document
object. It allows JDOM to interface easily with tools that construct DOM trees.Builders are also being developed that construct JDOM
Document
objects from SQL queries, LDAP queries, and other data formats. So, once in memory, documents are not tied to their build tool.Examples with SAX builder:
1. String source
String data = "<root>" + "<Companyname>" + "<Employee name=\"Girish\" Age=\"25\">Developer</Employee>" + "</Companyname>" + "<Companyname>" + "<Employee name=\"Komal\" Age=\"25\">Administrator</Employee>" + "</Companyname>" + "</root>"; SAXBuilder builder = new SAXBuilder(); Document document = builder.build(new ByteArrayInputStream(data.getBytes()));
2.From URL
SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(url);
Validation
The
SAXBuilder
and DOMBuilder
constructors let the user specify if validation should be turned on, as well as which parser class should perform the actual parsing duties. public SAXBuilder(String parserClass, boolean validation); public DOMBuilder(String adapterClass, boolean validation);
The defaults are to use Apache's open source Xerces parser and to turn off validation. Notice that the
DOMBuilder
doesn't take a parserClass
but rather an adapterClass
. That is because not all DOM parsers have the same API. To still allow user-pluggable parsers, JDOM uses an adapter class that has a common API for all DOM parsers. Adapters have been written for all the popular DOM parsers, including Apache's Xerces, Crimson, IBM's XML4J, Sun's Project X, and Oracle's parsers V1 and V2. Each one implements that standard interface by making the right method calls on the backend parser. That works somewhat similarly to JAXP (Resources) except it supports newer parsers that JAXP does not yet support.
No comments:
Post a Comment