If you have created a DOM document builder or a SAX parser using
the JAXP interfaces, you may have difficulty setting features and
properties directly using those interfaces. The following
instructions tell you how to set properties on document builders
and SAX parsers created from the JAXP interfaces.
The DocumentBuilderFactory interface contains a
setAttribute(String,Object)
method which may
provide a means to set features and properties on the underyling
parser. However, it cannot be relied upon. Therefore, you must
use the Xerces DOMParser object directly. For example:
 |  |  |
 | import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
DOMParser parser = new DOMParser();
String id = "http://apache.org/xml/properties/dom/document-class-name";
Object value = "org.apache.xerces.dom.DocumentImpl";
try {
parser.setProperty(id, value);
}
catch (SAXException e) {
System.err.println("could not set parser feature");
} |  |
 |  |  |
Using the SAXParser interface in JAXP is better because you can
query the underlying XMLReader implementation directly and that
interface contains methods to set and query features and
properties. For example:
 |  |  |
 | import javax.xml.parsers.SAXParser;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
SAXParser parser = /* created from SAXParserFactory */;
XMLReader reader = parser.getXMLReader();
String id = "http://apache.org/xml/properties/dom/document-class-name";
Object value = "org.apache.xerces.dom.DocumentImpl";
try {
reader.setProperty(id, value);
}
catch (SAXException e) {
System.err.println("could not set parser feature");
} |  |
 |  |  |