PDA

View Full Version : The difference beteen QDomNode and QDomElement



elcuco
6th January 2006, 10:26
I am writing an applications which uses a lot of XML for it's input. Until now I have been using QDomNode, and now I saw that there is a QDomElement class which can be used as well. I see that QDomElement is more suitable for me, since I can query it's sub childs
QDomElement::elementsByTagName() but this still returns to me a QDomNode list, which in turn can be converted to elements.

My question is why there is this separation between elements and nodes? Is it really needed? This just makes my code lines way larger.... :)

edit:
Lets give you an example of those monstrosities:


e.start = node.toElement().elementsByTagName("start-regex").item(0).toElement().text();

jacek
6th January 2006, 11:56
My question is why there is this separation between elements and nodes?
Because not every node is an element. Of course it would be simplier if would QDomElement::elementsByTagName() return list of elements, but this method was defined in DOM specification (http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614) like this:
interface Element : Node {
...
NodeList getElementsByTagName(in DOMString name);
...
};So the Trolls had not much choice.

munna
6th January 2006, 11:57
As the name suggests QDomNode gives you a node in the tree structure.
QDomNode will give u a particular node, its siblings, no of children etc....

QDomElement on the other hand does not have information about the neighbors. It knows only about the given node, its attributes, tags and the text.

Hope i have cleared your doubt.

Cheers

dfaure
6th January 2006, 17:27
The difference is that node can also be a comment, or a processing instruction, or text data.
So be careful when iterating over the result and calling toElement, it could fail on e.g. comments.

See the section QDomElement at the bottom of
http://developer.kde.org/documentation/other/mistakes.html

PS: previous reply is wrong; an element does know about its siblings since an element *is* a node.

munna
7th January 2006, 04:37
Ya, my mistake.