Solved Appending to xml
-
I think these might give u the answer u`re looking for
<Products> <Product> <prId>2506</prId> <spIcon>e19ea7d081f8782ac41269ac9268e52c.jpeg</spIcon> <prThumb>d044d922d380359eec7452cc751a9d2c.jpeg</prThumb> <prName> toilet tissue twinsaver baby soft 18s</prName> <saved>0</saved> <price>65.95</price> </Product> </Products>
These gives me failed(when appending):
if(append == true){ qDebug() << "appending"; file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text); if(doc.setContent(&file)){ qDebug() << "read."; }else{ qDebug() << "failed"; } root = doc.firstChildElement("Products"); }else{ qDebug() << "truncating.."; file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text); qDebug() << "file open.."; root = doc.createElement("Products"); doc.appendChild(root); }
Then
QDomElement productTag = doc.createElement("Product"); qDebug() << "appending Product tag"; root.appendChild(productTag );
-
@chawila said in Appending to xml:
firstChildElement
- Check the return result of
root = doc.firstChildElement("Products");
beforeroot.appendChild(productTag )
? - Try
QDomElement root = doc.documentElement();
instead ofdoc.firstChildElement("Products")
? - Do you understand that the whole principle of appending with
QIODevice::Append
to the document file in order to create a newProduct
is never going to work, because you need that new node to go inside theProducts
node in any case?
- Check the return result of
-
@JNBarchan
itried it still get:
Calling appendChild() on a null node does nothing.
So what are u suggesting aboutQIODevice::Append
. -
@chawila said in Appending to xml:
@JNBarchan
itried it still get:
Calling appendChild() on a null node does nothing.
So for your root.appendChild(productTag ), debug out both
root
&productTag
?So what are u suggesting about
QIODevice::Append
.For what you want to do, you cannot use
root.appendChild(productTag )
and you cannot "append to the existing XML in the file" in any shape or form. I think from what you're saying you want to:- Read in the whole existing XML file to create the document.
- Do your code which alters the DOM by appending new nodes or whatever.
- Overwrite the complete file with the serialization of the complete DOM document, i.e. no appending, just like creating it from scratch.
-
@JNBarchan
I got compilation Error:No match for 'operator'<<(operand types are 'QDeburg' and QDomElement') qDebug() << root << productTag ;
So how can i do that?..
-
Read in the whole existing XML file to create the document.
-
Do your code which alters the DOM by appending new nodes or whatever.
-
Overwrite the complete file with the serialization of the complete DOM document, i.e. no appending, just like creating it from scratch.
-
-
@chawila
Outline code:if (append) { openFileForReadOnly(); readFileToCreateDOM(); closeFile(); } else { createNewEmptyDom(); possiblyCreateEmptyProductsNode(); } doCodeToPutNewProductNodesUnderProductsNode(); saveWholeDOMDocumentToFileWithOverwrite();
No
ReadWrite
s orAppend
s ;Truncate
only used insaveWholeDOMDocumentToFileWithOverwrite()
if that's what is required to overwrite. See e.g. https://forum.qt.io/topic/43724/solved-save-qdomdocument-to-xml or http://www.qtforum.org/article/2756/how-to-qdomdocument-to-file.html. e.g.file.open( QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text )
when you're ready to overwrite. -
@JNBarchan
So all these:
openFileForReadOnly(); readFileToCreateDOM(); //----------------------- createNewEmptyDom(); possiblyCreateEmptyProductsNode(); //-------------------- doCodeToPutNewProductNodesUnderProductsNode(); saveWholeDOMDocumentToFileWithOverwrite();
Are methods/functions i should create
-
@chawila
Well, yes!
For:createNewEmptyDom(); possiblyCreateEmptyProductsNode();
you already had:
root = doc.createElement("Products"); doc.appendChild(root);
(provided your DOM doc starts out empty).
BTW
QIODevice::WriteOnly 0x0002 The device is open for writing. Note that this mode implies Truncate.
-
@JNBarchan
Ok i`ll keep on trying,
but u are giving me a big task(reaserch
) to do.. -
@chawila
Sorry, I don't see any "big task" or "research" for you to do?
You already seem to know how to read a file to QDomDocument, add nodes to it, and write it out, that's all we're doing. -
@JNBarchan
Let me get into it i`ll be back, hopefully with good news.. -
@chawila
It is your use of "append to xml file" that is faulty. To add new nodes, you do not literally "append" them to the whole file, you add (append/insert) them near the end of the file but not literally at the end of the file.P.S.
You can probably useQDomDocument::save(QTextStream)
as one way of saving the dom doc back to file, if you like. -
@JNBarchan
Thanks,
it was little bit challenging though.. -
@chawila
Well, I'm sorry but "appending to an XML file" is "a bit challenging", because as you've seen you don't quite want to do that... :)