Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calling appendChild on a QDomDocument that already has an element node as a child

Calling appendChild on a QDomDocument that already has an element node as a child

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on last edited by
    #1

    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 from otherDoc to doc:

    doc.appendChild(otherDoc.firstChildElement());
    

    The result seems to be that doc now displays the following when calling toString() on it:

    <foo/>
    <bar/>
    

    So it looks like the appendChild operation somehow 'worked' (although we are now left with a QDomDocument without a root element... or with two root elements...) This is in contradiction with the documentation, according to which the appendChild 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)

    jsulmJ JonBJ 2 Replies Last reply
    0
    • B Bart_Vandewoestyne

      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 from otherDoc to doc:

      doc.appendChild(otherDoc.firstChildElement());
      

      The result seems to be that doc now displays the following when calling toString() on it:

      <foo/>
      <bar/>
      

      So it looks like the appendChild operation somehow 'worked' (although we are now left with a QDomDocument without a root element... or with two root elements...) This is in contradiction with the documentation, according to which the appendChild 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)

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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

        B Offline
        B Offline
        Bart_Vandewoestyne
        wrote on last edited by
        #3

        @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 a QDomDocument that already has root element. Since QDomDocument inherits from QDomNode, and the documentation at http://doc.qt.io/archives/qt-4.8/qdomnode.html#appendChild says

        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 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...

        jsulmJ 1 Reply Last reply
        0
        • B Bart_Vandewoestyne

          @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 a QDomDocument that already has root element. Since QDomDocument inherits from QDomNode, and the documentation at http://doc.qt.io/archives/qt-4.8/qdomnode.html#appendChild says

          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 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...

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Bart_Vandewoestyne OK, understand. Maybe the documentation isn't correct here.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Bart_Vandewoestyne

            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 from otherDoc to doc:

            doc.appendChild(otherDoc.firstChildElement());
            

            The result seems to be that doc now displays the following when calling toString() on it:

            <foo/>
            <bar/>
            

            So it looks like the appendChild operation somehow 'worked' (although we are now left with a QDomDocument without a root element... or with two root elements...) This is in contradiction with the documentation, according to which the appendChild 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)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved