gpt4 book ai didi

java - SAXBuilder().build(InputStream) - 这会将整个文件读入内存吗?

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

阅读文档,这是我见过的所有示例中使用的方法:

(org.jdom.input.SAXBuilder 的版本是jdom-1.1.jar)

Document doc = new SAXBuilder().build(is);
Element root = doc.getRootElement();
Element child = root.getChild("someChildElement");
...

其中是一个InputStream变量。

我想知道,由于这是一个 SAX 构建器(与 DOM 构建器相对),是否使用构建方法将整个输入流读入文档对象?或者它是不是在延迟加载,只要我请求具有 Element.getChildren() 或类似功能(源于根节点)的元素,这些元素只能通过文档转发,那么构建器自动为我加载流的 block ?

我需要确定我没有将整个文件加载到内存中。

谢谢,迈克

最佳答案

DOM 解析器与 JDom 解析器类似,将整个 XML 资源加载到内存中,为您提供一个 Document 实例,允许在 XML 元素中导航。
一些引用 here :

the DOM standard is a codified standard for an in-memory document model.

here :

JDOM works on the logical in-memory XML tree,

DOM 和 JDom 都在内部使用 SAX 解析器来读取 XML 资源,但它们仅使用它来将整个内容存储在它们返回的 Document 实例中。事实上,使用 Dom 和 JDom,客户端永远不需要提供处理程序来拦截 SAX 解析器触发的事件。

请注意,DOM 和 JDom 都没有义务在内部使用 SAX。
他们主要使用它们,因为 SAX 标准已经存在,因此使用它来报告错误是有意义的。


I need to be sure I'm not loading the whole file into memory.

使用 XML 有两种编程模型:流式处理和文档对象模型 (DOM)。
你正在寻找第一个。

所以使用SAX parser通过提供您的处理程序来处理 SAX 解析器生成的事件(startDocument()startElement() 等),或者作为替代方案查看对用户更友好的 API: STAX (用于 XML 的流式 API):

As an API in the JAXP family, StAX can be compared, among other APIs, to SAX, TrAX, and JDOM. Of the latter two, StAX is not as powerful or flexible as TrAX or JDOM, but neither does it require as much memory or processor load to be useful, and StAX can, in many cases, outperform the DOM-based APIs. The same arguments outlined above, weighing the cost/benefits of the DOM model versus the streaming model, apply here.

关于java - SAXBuilder().build(InputStream) - 这会将整个文件读入内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50687326/

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