简体   繁体   English

后端的 HTML 验证

[英]HTML Validation on back-end

I am receiving response from external service in html format and pass it directly to my front end.我正在以 html 格式接收来自外部服务的响应并将其直接传递给我的前端。 However, sometime external system returns broken html, which can lead to the broken page on my site.但是,有时外部系统会返回损坏的 html,这可能会导致我网站上的页面损坏。 Thence, I want to validate this html response whether it is broken or valid.因此,我想验证这个 html 响应是否已损坏或有效。 If it is valid I will pass it further, otherwise it will be ignored with error in log.如果它是有效的,我会进一步传递它,否则它将被忽略并在日志中出现错误。

By what means can I make validation on back-end in Java?我可以通过什么方式在 Java 中对后端进行验证?

Thank you.谢谢你。

I believe there is no such "generic" thing available in Java.我相信 Java 中没有这种“通用”的东西。 But you can build your own parser to validate the HTML using any one Open Source HTML Parser但是您可以使用任何一种开源 HTML 解析器构建自己的解析器来验证HTML

I found the solution:我找到了解决方案:

private static boolean isValidHtml(String htmlToValidate) throws ParserConfigurationException, 
        SAXException, IOException {
    String docType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " +
            "\"https://www.w3.org/TR/xhtml11/DTD/xhtml11-flat.dtd\"> " +
            "<html xmlns=\"http://www.w3.org/1999/xhtml\" " + "xml:lang=\"en\">\n";

    try {
        InputSource inputSource = new InputSource(new StringReader(docType + htmlToValidate));

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setValidating(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {
            @Override
            public void error(SAXParseException exception) throws SAXException {
                throw new SAXException(exception);
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                throw new SAXException(exception);
            }

            @Override
            public void warning(SAXParseException exception) throws SAXException {
                throw new SAXException(exception);
            }
        });

        builder.parse(inputSource);
    } catch (SAXException ex) {
        //log.error(ex.getMessage(), ex); // validation message
        return false;
    }

    return true;
}

This method can be used this way:这个方法可以这样使用:

  String htmlToValidate = "<head><title></title></head><body></body></html>";

  boolean isValidHtml = isValidHtml(htmlToValidate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM