What is wrong exactly? Saying "it doesn't work" doesn't provide any hints as to what is wrong. One thing might be that you don't have a root element in your xml.
What is wrong exactly? Saying "it doesn't work" doesn't provide any hints as to what is wrong. One thing might be that you don't have a root element in your xml.
ya i also think so , so that i am now generating xml with rrot element through DOM parser, but when i create file then it is ok but when i want to add new entries it is not getting append to the root node in xml file.
My code for this is
Qt Code:
const int Indent = 4; static QDomDocument doc; static QDomElement root; QDomElement statement, action_node, condition_node; root = doc.createElement("root"); if(!file->exists()) { doc.appendChild(root); } else { statement = doc.createElement("statement"); condition_node = doc.createElement("condition"); action_node = doc.createElement("action"); root.appendChild(statement); statement.appendChild(condition_node); statement.appendChild(action_node); condition_node.appendChild(condition_text); action_node.appendChild(action_text); } if(file->error()) qDebug()<<file->errorString(); doc.save(out, Indent);To copy to clipboard, switch view to plain text mode
what i am doing wrong now.
Last edited by wysota; 13th September 2011 at 13:41. Reason: missing [code] tags
You can't append to the file like that. You need to replace the contents of the file and not append to it.
Please be more elaborated, it is possible to give some sample code.
If you have a document and you modify it, you need to save the whole document to the file overwriting the old version of the document. XML doesn't work in an "additive" manner. It's a matter of understanding how XML works. If you open the file in append mode, you will get two documents one after the other instead of having the updated document only.
So what i have to do now , do i have to create temproryfile.
You have to read the original document from the file, update it with new entries and save it back again. No temporary files are needed.
Can you please give me some sample code. I have confused a lot.
Qt Code:
QDomDocument doc; doc.setContent(&file); root.appendChild(newOne); file.seek(0); doc.save(stream); // or file.write(doc.toByteArray());To copy to clipboard, switch view to plain text mode
Niamita (14th September 2011)
I tried as you suggested but now i am facing a problem when i close the program and then write on the file , it write nothing on the file but it is working fine when the program is running once. I am getting error that "Calling appendChild() on a null node does nothing. " what is wrong with this.
My code is:
Qt Code:
const int Indent = 4; static QDomDocument doc; static QDomElement root,root1; QDomElement statement, action_node, condition_node; root = doc.createElement("root"); if(!file->exists()) { doc.appendChild(root); } else { root1 = doc.documentElement(); statement = doc.createElement("statement"); condition_node = doc.createElement("condition"); action_node = doc.createElement("action"); statement.appendChild(condition_node); statement.appendChild(action_node); condition_node.appendChild(condition_text); action_node.appendChild(action_text); root1.appendChild(statement); file->seek(0); } doc.save(out, Indent)To copy to clipboard, switch view to plain text mode
It is solved. Actually i am not setting contents of document.
doc.setContent(file);
Last edited by Niamita; 14th September 2011 at 07:08.
Hi
How to update a textnode data in xml through QDomElement.
I have following code
So i want to update the textnode value while it is getting append in previous value.Qt Code:
static QDomDocument doc; static QDomElement root,root1; QDomElement statement, action_node, condition_node; QDomText id_text; root = doc.createElement("root"); if(!file->exists()) { id_text = doc.createTextNode("0"); root.appendChild(id); id.appendChild(id_text); doc.appendChild(root); } else { doc.setContent(file); root1 = doc.documentElement(); int a = string_data.toInt(); qDebug()<<string_data; string_data.clear(); id_text = doc.createTextNode("Qt"); // Here i want to write Qt but it is getting write 0Qt on the textnode. node.appendChild(id_text); statement = doc.createElement("statement"); condition_node = doc.createElement("condition"); action_node = doc.createElement("action"); statement.setAttribute("id", i); statement.appendChild(condition_node); statement.appendChild(action_node); condition_node.appendChild(condition_text); action_node.appendChild(action_text); root1.appendChild(statement); // QString str = statement.attribute("id"); // qDebug()<<str.toInt(); file->seek(0); } doc.save(out, Indent);To copy to clipboard, switch view to plain text mode
Please anyone help me.
Waiting for response.
Bookmarks