QXML
-
@#
QDomDocument document;
QDomElement root = document.createElement("main");
document.appendChild(root);QDomElement book = document.createElement("book"); book.setAttribute("Name ", ui->lineEdit->text()); book.setAttribute("ID ",ui->lineEdit1->text()); root.appendChild(book); QFile file("E:/z.xml"); if(!file.open(QIODevice::Append | QIODevice::Text )) { qDebug() <<"Failed to open file "; // return -1; } else { QTextStream stream(&file); stream << document.toString(); file.close(); qDebug() <<"finished"; }@
-
Hi and welcome to devnet,
Please enclose your code with coding tags. It wont be easily readable without them.
What do you want to append to what ?
-
[quote author="moxi" date="1368779763"]i code doesn't work if i want to append it.....
here is my code...[/quote]
well... beside you should use code tags you also need to be clearer. What doesn't work and what do you expect. Otherwise we can just guess... -
How should the XML file look like at the end (supposing, it would work)?
-
@SGaist i just want to append my file
so after giving input all the data should be in the single <main> tag
but when ever i give input my app makes a new <main> tag which is wrong.. -
Well, your function creates a new main tag each time.
You either have to read your xml back, append your new book to the document and write the whole thing back to the file or modify the end of the file yourself (bad idea).
-
@Sgait
jux give me the idea how to add all the records in the same <main> tag?? -
It depends on when you want to add the record. Are you updating your xml file or are you creating a new XML file ?
-
i am actually updating the xml file
-
Since you're using DOM, read your XML back, search for your main element and append a new "book" child to it. Once done overwrite your file with the new content of the document.
-
Can u plz give me a code example
or simply edit my code.. -
- Read the xml file in a QDomDocument -> in the doc of QDomDocument
- Get "main" element -> in the doc of QDomElement
- Add a new element -> you already know how
- Write the document -> overwrite the complete file
-
hmmmm let me try this