3

In Qt there are a number of different ways to work with XML. To keep this simple I only want to look at the QXml* classes and the QDom* classes.

I'm trying to figure out which one to use but they both look to have similar functionality.

What's are the main differences between QXml and QDom?

Hypothetical example: Does one read the whole xml file into memory making it slow at startup but faster after startup?

What scenarios should require you to you to use one method over the other? and why should you use one over the other?

Hypothetical example: let's say you you are doing a "one-pass" versus "multi-pass"...

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177

1 Answers1

6

In short, QXml* classes implement SAX (Simple API for XML) XML parser while QDom* implement DOM (Document Object Model) XML parser.

The main difference is that SAX is a sequential access parser, so it parses the document as it reads it, and makes first chunks of parsed data available almost instantly. DOM needs to load the whole document into the memory to get it parsed, but it might be a bit easier to handle in terms of code overhead (for SAX you have to implement XML handler class). In general, SAX is more lightweight and faster.

There's lots of reading online regarding comparison of SAX and DOM:

why is sax parsing faster than dom parsing ? and how does stax work?

http://developerlife.com/tutorials/?p=28

And here's a nice document comparing various multiplatform XML parsers (including QXml* and QDom*). Your best choice depends on your use case, if you're working with huge XML documents, you'd prefer SAX. For tiny XMLs you'd be better off using DOM, since it's just a few lines of code to get data you need from a file.

Community
  • 1
  • 1
ayoy
  • 3,835
  • 19
  • 20
  • Maybe a good rule of thumb is start with QDom because the code is easier and if performance/memory becomes an issue try using QXml/SAX? – Trevor Boyd Smith Oct 20 '11 at 21:21
  • Yes, that might be the right approach. Though as for me, I personally prefer SAX API, but maybe just because I'm using it for a longer time now. Anyway, you can start off with DOM in really 5 lines of code or so, just check Qt examples. – ayoy Oct 20 '11 at 21:43
  • KISS for now. Less code is better for now in early stages. Later I will reevaluate. (BTW all 3 of your links are amazing.) – Trevor Boyd Smith Oct 21 '11 at 00:56
  • http://itee.uq.edu.au/~dasfaa/workshop/acceptedPaper/BenchmarX/submission_02.pdf is a better reference for the comparison, since docstoc.com does not really seem to be legit ... – maxschlepzig Nov 18 '12 at 17:18