Results 1 to 3 of 3

Thread: QDomNode::replaceChild Question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: QDomNode::replaceChild Question

    The code you have provided does not check the return value of n.replaceChild() at all. It would be more helpful if you could provide the actual code you are using. In any case, you are replacing e2, that is once you've called n.replaceChild(elem, e2); the node referenced by e2 is no longer a child of node n and so continuing to iterate over it will fail. What you probably want is to put e2 = elem; after the replace call.

  2. #2
    Join Date
    Sep 2010
    Location
    Washington, PA
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QDomNode::replaceChild Question

    Wow... that was a stupid mistake. It essentially is like a link in a link list. Once you append the element into the list of XML tags, that element becomes the new link in the chain, as the previous element was replaced by the new element. Therefore, the links to the previous and next sibling elements are null pointers. Therefore, by doing the following, I got it to work with your recommendation. (oh, I did have it returning e2 = n.replaceChild(elem, e2), but that was incorrect, as it needs to be the next sibling element. I just didn't have it reflected in the code I posted above, as it was just a way to quickly convey what I was trying to express).

    Qt Code:
    1. QDomNode n = root.firstChild();
    2. while(!n.isNull())
    3. {
    4. QDomElement e = n.toElement();
    5. if(!e.isNull())
    6. {
    7. if((e.tagName() == "config") and (e.attribute("name", "") == "Eddie"))
    8. {
    9. QDomElement e2 = n.firstChildElement();
    10. while(!e2.isNull())
    11. {
    12. if(e2.tagName() == "execdir")
    13. {
    14. QDomElement elem = doc.createElement("execdir");
    15. elem.appendChild(doc.createTextNode("EXECUTABLE DIRECTORY"));
    16. e2 = n.replaceChild(elem, e2).nextSiblingElement();
    17. }
    18. else if(e2.tagName() == "binary")
    19. {
    20. QDomElement elem = doc.createElement("binary");
    21. elem.appendChild(doc.createTextNode("EXECUTABLE"));
    22. e2 = n.replaceChild(elem, e2).nextSiblingElement();
    23. }
    24. else e2 = e2.nextSiblingElement();
    25. }
    26. }
    27. }
    28.  
    29. n = n.nextSibling();
    30. }
    To copy to clipboard, switch view to plain text mode 


    That now works, as stated. Just thought I would post the final code for anyone else looking at the same dumb mistake I was making.

Similar Threads

  1. Problem with Deleting QDomNode
    By metdos in forum Qt Programming
    Replies: 6
    Last Post: 16th July 2010, 19:58
  2. QDomNode
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 07:11
  3. QDomNode
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 13:18
  4. The difference beteen QDomNode and QDomElement
    By elcuco in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2006, 04:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.