SAX 和 DOM 有什么区别? [英] What is the difference between SAX and DOM?

查看:34
本文介绍了SAX 和 DOM 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些关于 XML 解析器的文章,并遇到了 SAXDOM.

I read some articles about the XML parsers and came across SAX and DOM.

SAX 是基于事件的,而 DOM 是树模型——我不明白这些概念之间的区别.

SAX is event-based and DOM is tree model -- I don't understand the differences between these concepts.

据我所知,基于事件的意味着某种事件发生在节点上.就像单击特定节点时,它将提供所有子节点,而不是同时加载所有节点.但是在 DOM 解析的情况下,它将加载所有节点并制作树模型.

From what I have understood, event-based means some kind of event happens to the node. Like when one clicks a particular node it will give all the sub nodes rather than loading all the nodes at the same time. But in the case of DOM parsing it will load all the nodes and make the tree model.

我的理解正确吗?

如果我错了,请纠正我或以更简单的方式向我解释基于事件和树模型.

Please correct me If I am wrong or explain to me event-based and tree model in a simpler manner.

推荐答案

好吧,您已经接近了.

在 SAX 中,当 XML 被解析时会触发事件.当解析器解析 XML 并遇到标记开始(例如 )时,它会触发 tagStarted 事件(事件的实际名称可能不同).类似地,当在解析时遇到标签的结尾 (</something>),它会触发 tagEnded.使用 SAX 解析器意味着您需要处理这些事件并理解每个事件返回的数据.

In SAX, events are triggered when the XML is being parsed. When the parser is parsing the XML, and encounters a tag starting (e.g. <something>), then it triggers the tagStarted event (actual name of event might differ). Similarly when the end of the tag is met while parsing (</something>), it triggers tagEnded. Using a SAX parser implies you need to handle these events and make sense of the data returned with each event.

在 DOM 中,解析时不会触发任何事件.解析整个 XML 并生成并返回 DOM 树(XML 中的节点).解析后,用户可以导航树以访问先前嵌入在 XML 中的各个节点中的各种数据.

In DOM, there are no events triggered while parsing. The entire XML is parsed and a DOM tree (of the nodes in the XML) is generated and returned. Once parsed, the user can navigate the tree to access the various data previously embedded in the various nodes in the XML.

一般来说,DOM 更易于使用,但在您开始使用它之前需要解析整个 XML.

In general, DOM is easier to use but has an overhead of parsing the entire XML before you can start using it.

这篇关于SAX 和 DOM 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆