View Full Version : DOM setContent first or last?

12th December 2011, 19:20
When reading an XML file, which of the following methods should I use?

(1) Open the file, .setContent(file), parse it in nested loops where I check tagnames with string comparisons and move along with .nextSibling() and .firstChild().

(2) Construct a tree object starting with the root element and adding child elements as I expect them to appear in the file, open the file, .setContent(file), and BAM the previously contructed element objects (and their attributes) now contain the data I need.

If (2) is advisable I could use sample code for the first few elements... how are lists containing an unkown amount of elements with the same tagname (different attribute values) handled?

13th December 2011, 09:04
What exactly do you want to do with the data? Because (2) seems pretty useless for me, because that is what QDomDocument already does. Your can use QDomDocument::elementsByTagName() or similar to get the value of a specific node or use XQuery.

13th December 2011, 10:55
Well, I'd put some data to non-xml member variables and display some directly in the UI... The idea with (2) is that unlike in (1), the data would be in local variables (QDomElement objects or maybe lists thereof) whose names I'd already know, thereby removing the need to traverse the structure with if-tagname comparisons and while-nextsibling loops.