Hello everyone, and first off thanks a lot for reading this. I'm a QT newbie who needs to add additional content (nodes) to an existing XML document. Currently I'm doing some simple tests, involving parsing the original XML doc (using DOM), creating a new document, appending the existing and new content to it, and then writing everything to my file.

Unfortunately I'm experiencing some very weird behavior because the nextSibling method doesn't seem to work properly when appendChild is also used. It looks like someone else in the past also stepped into this (http://bit.ly/d45pT3), but unluckily I haven't been able to get through this or find a solution, so that I'm wondering if someone here can help me out.

My (stripped down version of my) code is what follows:

Qt Code:
  1. // My file, a constant
  2. QFile file(DATA_FILE);
  3.  
  4. // DOM Structure for new document is created
  5. QDomDocument newdoc( "exampledoc" );
  6. QDomElement newroot = newdoc.createElement( "itemlist" );
  7. newdoc.appendChild(newroot);
  8.  
  9. // Current document is read and parsed
  10. file.open( QIODevice::ReadOnly )
  11.  
  12. QDomDocument doc( "exampledoc" );
  13. doc.setContent( &file )
  14.  
  15. // Existing document root - itemlist
  16. QDomElement root = doc.documentElement();
  17.  
  18. // first child element - exampleitem
  19. QDomNode n = root.firstChild();
  20.  
  21. while( !n.isNull() )
  22. {
  23. QDomElement e = n.toElement();
  24. if( !e.isNull() )
  25. {
  26. if( e.tagName() == "exampleitem" )
  27. {
  28. // Existing Nodes Are Added
  29. // IF FOLLOWING LINE IS COMMENTED PARSING CONTINUES AS EXPECTED
  30. // WITH NEXT SIBLING, ELSE n.nextSibling() BELOW RETURNS NULL
  31. //AND CYCLE IS EXITED
  32. newroot.appendChild(n);
  33. }
  34. }
  35.  
  36. // Hopefully next exampleitem is taken in consideration
  37. // does NOT work when appendChild above is active
  38. n = n.nextSibling();
  39.  
  40. } // End while
  41.  
  42. // Input file is closed
  43. file.close();
  44.  
  45. // New Item for the new document is created
  46. QDomElement newitem = newdoc.createElement( "exampleitem" );
  47.  
  48. /* Here I add some stuff (children items ) to newitem - always working, so no code displayed */
  49.  
  50. // New Item is Appended to XML
  51. newroot.appendChild(newitem);
  52.  
  53. // File is reopened, for write this time
  54. file.open( QIODevice::WriteOnly )
  55. newdoc.appendChild( newroot);
  56.  
  57. // Content is written to file (actually works because
  58. // new document is in fact written to file)
  59. QTextStream ts( &file );
  60. ts << newdoc.toString();
  61.  
  62. file.close();
To copy to clipboard, switch view to plain text mode 

The problem is that nextSibling on line 38 only works if I comment appendChild on line 32, which seems a weird situation to me. I am probably missing something important here.

I was therefore wondering if there was some known issue (that I haven't been able to find) about the use of the two methods above, if I was simply doing something wrongly or, alternatively if someone could be so kind to point me to some tutorial/source, that I can use as a reference for seeing how to add elements to an existing XML document using a better/recommended approach.

Once again, I thank you for your attention and patience.