Download Xerces from xml.apache.org. Place the relevant JAR's on your classpath. Here is sample code to get a DOM document reference.
- Saish
public final class DomParser extends Object {
/////////////////////
// Class Variables //
/////////////////////
private static final DocumentBuilder builder;
private static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
/** W3C schema definitions */
private static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
//////////////////
// Constructors //
//////////////////
static {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
System.err.println("[warning] "+e.getMessage());
}
public void error(SAXParseException e) throws SAXException {
System.err.println("[error] "+e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
System.err.println("[fatal error] "+e.getMessage());
throw new XmlParsingError("Fatal validation error", e);
}
});
}
catch (ParserConfigurationException fatal) {
throw new ConfigurationError("Unable to create XML DOM document parser", fatal);
}
catch (FactoryConfigurationError fatal) {
throw new ConfigurationError("Unable to create XML DOM document factory", fatal);
}
}
private DomParser() {
super();
}
////////////////////
// Public Methods //
////////////////////
public static final Document newDocument() {
return builder.newDocument();
}
public static final Document parseDocument(final InputStream in) {
try {
return builder.parse(in);
}
catch (SAXException e) {
throw new XmlParsingError("SAX exception during parsing. Document is not well-formed or contains " +
"illegal characters", e);
}
catch (IOException e) {
throw new XmlParsingError("Encountered I/O exception during parsing", e);
}
}
}
- Saish