Unsolved Calling appendChild on a QDomDocument that already has an element node as a child
-
For Qt 4.8.7, the documentation at http://doc.qt.io/archives/qt-4.8/qdomnode.html#appendChild reads:
If newChild is a QDomElement and this node is a QDomDocument that already has an element node as a child, newChild is not added as a child and a null node is returned.
I wanted to confirm that, so I create a
QDomDocument
:QDomDocument doc; doc.setContent(QString("<foo/>"));
I then create another
QDomDocument
:QDomDocument otherDoc; otherDoc.setContent(QString("<bar/>"));
I then append the
QDomElement
document element fromotherDoc
todoc
:doc.appendChild(otherDoc.firstChildElement());
The result seems to be that
doc
now displays the following when callingtoString()
on it:<foo/> <bar/>
So it looks like the
appendChild
operation somehow 'worked' (although we are now left with aQDomDocument
without a root element... or with two root elements...) This is in contradiction with the documentation, according to which theappendChild
should fail and not add the<bar/>
element. Or am I misinterpreting something???(See also my testprogram at https://github.com/BartVandewoestyne/Qt/tree/master/examples/qdomdocument)
-
@Bart_Vandewoestyne Shouldn't you append the element to the root element? You can get it via http://doc.qt.io/qt-5/qdomdocument.html#documentElement
-
@jsulm said in Calling appendChild on a QDomDocument that already has an element node as a child:
@Bart_Vandewoestyne Shouldn't you append the element to the root element? You can get it via http://doc.qt.io/qt-5/qdomdocument.html#documentElement
I could do that, but that is not really my question :-) I want to see what happens if you call
appendChild
on aQDomDocument
that already has root element. SinceQDomDocument
inherits fromQDomNode
, and the documentation at http://doc.qt.io/archives/qt-4.8/qdomnode.html#appendChild saysIf newChild is a QDomElement and this node is a QDomDocument that already has an element node as a child, newChild is not added as a child and a null node is returned.
I was expecting that my
<bar/>
element would not have been added... but apparently, it is... and I don't understand why. It seems like what I see is not what the documentation says... -
@Bart_Vandewoestyne OK, understand. Maybe the documentation isn't correct here.
-
@Bart_Vandewoestyne said in Calling appendChild on a QDomDocument that already has an element node as a child:
doc.appendChild(otherDoc.firstChildElement());
I don't know whether I'm right on this or this is relevant. I don't use
QDomDocument
. But in all the "XmlDocument" frameworks I have used elsewhere, if you try to insert an XmlNode into an XmlDocument you can only do so provided the node to insert was created in the same XmlDocument. If it comes from a different XmlDocument you must instead import it from the other document to the current one, e.g.ImportNode
in .NET?