与DOM和SAX相比,XPath的效率更高吗? [英] Is XPath much more efficient as compared to DOM and SAX?

查看:389
本文介绍了与DOM和SAX相比,XPath的效率更高吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析一个xml字符串,并找到特定文本节点,属性值等的值。
我在javascript中这样做,并且使用DOMParser类。后来我被告知DOM占用大量的内存,SAX是一个更好的选择。

I need to parse an xml string and find values of specific text nodes, attribute values etc. I'm doing this in javascript and was using the DOMParser class for the same. Later I was informed that DOM is takes up a lot of memory and SAX is a better option.

最近我发现XPath也提供了一种查找节点的简单方法。

Recently I found that XPath too provides a simple way to find nodes.

但我不知道这3个中哪一个将是解析XML的最有效的方法。
请帮助....

But I'm not sure which amongst these 3 would be the most efficient way to parse XML. Kindly help....

推荐答案

SAX是自上而下的解析器,允许对XML文档进行串行访问,并且可以只读访问。另一方面,DOM更强大 - 它将整个XML文档读入树中,并且当您要更改,添加,删除该XML树中的数据时,效率非常高。当您只需要XML文档中的几个值时,XPath就很有用,并且您知道在哪里找到它们(您知道数据的路径,/ root / item / challange / text)。

SAX is a top-down parser and allows serial access to a XML document, and works well for read only access. DOM on the other hand is more robust - it reads the entire XML document into a tree, and is very efficient when you want to alter, add, remove data in that XML tree. XPath is useful when you only need a couple of values from the XML document, and you know where to find them (you know the path of the data, /root/item/challange/text).

SAX:迭代文档时有效率,为每次迭代提供一次通过

SAX: Time efficient when iterating through the document, gives a single pass for every iteration

DOM:灵活/性能,为您提供更多方法工作您的数据

DOM: Flexible/performance, gives you more ways to work your data

XPath:只需要读取几个值时才有效率

XPath: Time efficient when you only need to read a couple of values

这篇关于与DOM和SAX相比,XPath的效率更高吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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