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:
// My file, a constant
// DOM Structure for new document is created
QDomElement newroot
= newdoc.
createElement( "itemlist" );
newdoc.appendChild(newroot);
// Current document is read and parsed
doc.setContent( &file )
// Existing document root - itemlist
// first child element - exampleitem
while( !n.isNull() )
{
if( !e.isNull() )
{
if( e.tagName() == "exampleitem" )
{
// Existing Nodes Are Added
// IF FOLLOWING LINE IS COMMENTED PARSING CONTINUES AS EXPECTED
// WITH NEXT SIBLING, ELSE n.nextSibling() BELOW RETURNS NULL
//AND CYCLE IS EXITED
newroot.appendChild(n);
}
}
// Hopefully next exampleitem is taken in consideration
// does NOT work when appendChild above is active
n = n.nextSibling();
} // End while
// Input file is closed
file.close();
// New Item for the new document is created
QDomElement newitem
= newdoc.
createElement( "exampleitem" );
/* Here I add some stuff (children items ) to newitem - always working, so no code displayed */
// New Item is Appended to XML
newroot.appendChild(newitem);
// File is reopened, for write this time
newdoc.appendChild( newroot);
// Content is written to file (actually works because
// new document is in fact written to file)
ts << newdoc.toString();
file.close();
// My file, a constant
QFile file(DATA_FILE);
// DOM Structure for new document is created
QDomDocument newdoc( "exampledoc" );
QDomElement newroot = newdoc.createElement( "itemlist" );
newdoc.appendChild(newroot);
// Current document is read and parsed
file.open( QIODevice::ReadOnly )
QDomDocument doc( "exampledoc" );
doc.setContent( &file )
// Existing document root - itemlist
QDomElement root = doc.documentElement();
// first child element - exampleitem
QDomNode n = root.firstChild();
while( !n.isNull() )
{
QDomElement e = n.toElement();
if( !e.isNull() )
{
if( e.tagName() == "exampleitem" )
{
// Existing Nodes Are Added
// IF FOLLOWING LINE IS COMMENTED PARSING CONTINUES AS EXPECTED
// WITH NEXT SIBLING, ELSE n.nextSibling() BELOW RETURNS NULL
//AND CYCLE IS EXITED
newroot.appendChild(n);
}
}
// Hopefully next exampleitem is taken in consideration
// does NOT work when appendChild above is active
n = n.nextSibling();
} // End while
// Input file is closed
file.close();
// New Item for the new document is created
QDomElement newitem = newdoc.createElement( "exampleitem" );
/* Here I add some stuff (children items ) to newitem - always working, so no code displayed */
// New Item is Appended to XML
newroot.appendChild(newitem);
// File is reopened, for write this time
file.open( QIODevice::WriteOnly )
newdoc.appendChild( newroot);
// Content is written to file (actually works because
// new document is in fact written to file)
QTextStream ts( &file );
ts << newdoc.toString();
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.
Bookmarks