如何在Java中验证XHTML 1.1

时间:2014-05-08 23:36:13

标签: java validation xhtml xsd dtd

我试图找到验证XHTML 1.1文档的方法。我有一个方法可以针对XSD验证XML,而针对DTD验证另一个。

DTD适用于XHTML 1.0 Strict,但使用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;
        }
}

另一个人总是说即使我从W3C网站上拿这个例子也无效:

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());
  }

如何在Java中验证XHTML 1.1?

我展示了我尝试过但没有任何效果。

有人看到我的错误,或者有解决方案吗?

1 个答案:

答案 0 :(得分:0)

使用以下过程:

  • 将DTD转换为XML序列化
  • 将XML序列化转换为XSD
  • 使用本地XSD重试

<强>参考