如何禁用Java中显示的致命错误?

发布于 2025-01-31 11:44:42 字数 425 浏览 0 评论 0原文

我想知道是否可以禁用 [致命错误]:1:50:在我的Java程序中,PublicId和SystemID之间需要白色空间。错误。

我使用Java内置DOM Parser API来解析XML文档。 XML文档故意不正确,因为我想学习如何处理与XML解析有关的异常。我还想说,当XML文档正确时,没有例外。

我已经尝试了以下内容:

try {
    Document doc = documentBuilder.parse(xmlDocument)
} catch (Throwable t){
     System.out.println("An error occur when parsing");
}

正确显示捕获块内部的代码,但仍显示致命错误消息。

如果有人有解决方案,我会很高兴,谢谢。

I would like to know if it is possible to disable the [Fatal Error]:1:50: White spaces are required between publicId and systemId. error in my java program.

Im using java built-in DOM parser api to parse an XML document. The xml document is deliberately incorrect because I want to learn how to handle exceptions related to xml parsing. I also want to say that when the xml document is correct, no exception occurs.

I have already tried with the following :

try {
    Document doc = documentBuilder.parse(xmlDocument)
} catch (Throwable t){
     System.out.println("An error occur when parsing");
}

The code inside the catch block is displayed correctly but the fatal error message is still showing.

If anyone has a solution, I would be delighted, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2025-02-07 11:44:42

问题是默认情况下,错误处理程序对于XML解析器将错误打印到输出。您可以安装自己的处理程序以捕获或忽略错误:

class NullErrorHandler implements ErrorHandler {
    @Override
    public void fatalError(SAXParseException e) {
        // do nothing
    }

    @Override
    public void error(SAXParseException e) {
        // do nothing
    }
    
    @Override
    public void warning(SAXParseException e) {
        // do nothing
    }
}

使用如下:

documentBuilder.setErrorHandler(new NullErrorHandler());

您还可以有一个错误处理程序,例如,在成员变量中将列表存储在列表中,因此您可以在解析后访问它们并检查它们文档。

这是一个完整的示例:

import javax.xml.parsers.*;
import org.xml.sax.*;

String xml = """
             <?xml version="1.0"?>
             <!DOCTYPE whatsupdoc PUBLIC
                 "http://example.com/bugs""http://example.com/bunny">
             """;

InputSource getXmlInput(String xml) {
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    return is;
}

var documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

documentBuilder.parse(getXmlInput(xml));  // prints the error and throws
documentBuilder.setErrorHandler(new NullErrorHandler());
documentBuilder.parse(getXmlInput(xml));  // only throws, does not print

The problem is that by default the error handler for the XML parser prints the errors to the output. You can install your own handler to capture or ignore the errors:

class NullErrorHandler implements ErrorHandler {
    @Override
    public void fatalError(SAXParseException e) {
        // do nothing
    }

    @Override
    public void error(SAXParseException e) {
        // do nothing
    }
    
    @Override
    public void warning(SAXParseException e) {
        // do nothing
    }
}

Use it as follows:

documentBuilder.setErrorHandler(new NullErrorHandler());

You could also have an error handler that, for example, stored the exceptions in lists in member variables, so you could access them and examine them after parsing the document.

Here's a fully worked-out example:

import javax.xml.parsers.*;
import org.xml.sax.*;

String xml = """
             <?xml version="1.0"?>
             <!DOCTYPE whatsupdoc PUBLIC
                 "http://example.com/bugs""http://example.com/bunny">
             """;

InputSource getXmlInput(String xml) {
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    return is;
}

var documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

documentBuilder.parse(getXmlInput(xml));  // prints the error and throws
documentBuilder.setErrorHandler(new NullErrorHandler());
documentBuilder.parse(getXmlInput(xml));  // only throws, does not print
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文