I'm trying to find a way to validate a XHTML 1.1 document. I have a method that validates XML against XSD and another against DTD.
The DTD one works with XHTML 1.0 Strict but runs forever with XHTML 1.1:
public static boolean validateAgainstDTD(String xhtml) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
// Ignore the fatal errors
@Override
public void fatalError(SAXParseException exception)
throws SAXException {
}
// Validation errors
@Override
public void error(SAXParseException e) throws SAXParseException {
System.out.println("Error at " + e.getLineNumber()
+ " line.");
System.out.println(e.getMessage());
System.exit(0);
}
// Show warnings
@Override
public void warning(SAXParseException err)
throws SAXParseException {
System.out.println(err.getMessage());
System.exit(0);
}
});
Document xmlDocument = builder.parse(new ByteArrayInputStream(xhtml.getBytes()));
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(System.out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
"xhtml11.dtd");
transformer.transform(source, result);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
and the other one always says that it is not valid even if I take the example from the W3C website:
try {
// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new ByteArrayInputStream(str.getBytes()));
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance: the XHTML 1.0 WXS schema
Source schemaFile = new StreamSource("http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd");
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
} catch (ParserConfigurationException e) {
// parser configuration excepion
System.out.println(e.getMessage());
} catch (IOException e) {
// io exception
System.out.println(e.getMessage());
} catch (SAXException e) {
// instance document is invalid!
System.out.println(e.getMessage());
}
How do I validate XHTML 1.1 in Java?
I showed what I tried and nothing worked.
Does anyone see a mistake on my part, or have a solution?