简体   繁体   English

DOM vs SAX Java

[英]DOM vs SAX Java

I have a big xml file that could be downloaded from the internet. 我有一个大的xml文件,可以从互联网上下载。 To parse it I tried using the DOM parser however it doesn't let me skip certain tags as it gives me an error. 为了解析它,我尝试使用DOM解析器但是它不允许我跳过某些标记,因为它给了我一个错误。 Is there a way around this? 有没有解决的办法? If i understood correctly the SAX parser allows you to skip tags whilst the DOM doesn't. 如果我理解正确,SAX解析器允许您跳过标签,而DOM则不允许。 Can someone kindly clarify this fact, as if that is the case, I can't understand what is the advantage of a DOM parser. 有人可以澄清这个事实,好像就是这样,我无法理解DOM解析器的优点是什么。 Thanks in advance. 提前致谢。

DOM was designed as a language-independent object model to hold any XML data, and as such is a large and complex system. DOM被设计为一种独立于语言的对象模型,用于存放任何XML数据,因此是一个庞大而复杂的系统。 It suits well the two-phase approach of first loading an XML document in, then performing various operations on it. 它非常适合首先加载XML文档,然后对其执行各种操作的两阶段方法。 SAX, on the other hand, was designed as a fairly light-weight system using a single-phase approach. 另一方面,SAX被设计为使用单相方法的相当轻量级的系统。 With SAX, user-specified operations are performed as the document is loaded. 使用SAX,在加载文档时执行用户指定的操作。 Some applications use SAX to generate a smaller object model, with uninteresting information filtered out, which is then processed similarly to DOM. 一些应用程序使用SAX生成较小的对象模型,过滤掉不感兴趣的信息,然后以类似于DOM的方式处理。 Note that although DOM and SAX are the well-known "standard" XML APIs, there are plenty of others available, and sometimes a particular application may be better off using a non-standard API. 请注意,尽管DOM和SAX是众所周知的“标准”XML API,但还有很多其他可用的API,有时使用非标准API可能会使特定应用程序更好。 With XML the important bit is always the data; 使用XML,重要的一点始终是数据; code can be rewritten. 代码可以重写。

Some quick points: 一些快速点:

  1. SAX is faster than DOM. SAX比DOM快。
  2. SAX is good for large documents because it takes comparitively less memory than Dom. SAX适用于大型文档,因为它比Dom占用更少的内存。
  3. SAX takes less time to read a document where as Dom takes more time. 当Dom需要更多时间时,SAX花费更少的时间来阅读文档。
  4. With SAX we can access data but we can't modify data.With Dom we can modify data. 使用SAX,我们可以访问数据但我们无法修改数据。使用Dom我们可以修改数据。
  5. We can stop the SAX parsing when ever and where ever you want. 我们可以随时随地停止SAX解析。
  6. SAX is sequential parsing but with DOM we can move to back also. SAX是顺序解析,但是使用DOM我们也可以向后移动。
  7. To parse machine generated code SAX is better.To parse human readable documents DOM is useful. 要解析机器生成的代码SAX更好。要解析人类可读的文档,DOM很有用。

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

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