Search code examples
xmldomxpathsax

Is XPath much more efficient as compared to DOM and 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.

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

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


Solution

  • 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: Time efficient when iterating through the document, gives a single pass for every iteration

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

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