gpt4 book ai didi

java - 为什么 DocumentBuilder.parse 抛出 SaxParserException

转载 作者:行者123 更新时间:2023-11-30 09:07:21 25 4
gpt4 key购买 nike

从 xml 创建 DOM 的典型代码片段如下:

Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("somefile.xml");

我想知道为什么 parse 方法抛出 SAXException

我查看了代码,发现 DOMParserparse(InputSource inputSource) 方法最初将此异常向上抛出。

谁能帮我理解,为什么 DOM 创建需要我们捕获 SaxException ?是不是有点矛盾?

最佳答案

因为它在内部使用了一些 SAX API。

Note that this class reuses several classes from the SAX API. This does not require that the implementor of the underlying DOM implementation use a SAX parser to parse XML document into a Document. It merely requires that the implementation communicate with the application using these existing APIs.

http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html

关于java - 为什么 DocumentBuilder.parse 抛出 SaxParserException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24013492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com