Cantora (5th June 2009)
For my current logic.. I am only able to read until <parameters>. Now I still fail to make my code can read until <parameter>
so sorry.. after go thur the help file more details now i able to do it
QDomElement root = doc.documentElement();
QDomElement item = root.firstChildElement();
QDomElement sub = item.firstChildElement();
QString strTemp1 = sub.attribute("MODVERS");
after I change the MODVERS value how I going to update the XML file?
In firstChildElement() you can place the name of the tag you want to find. Don't rely on the order of tags...
As for changes, there is QDomDocument::toString() that you can dump to a file afterwards.
so if I want to save it back to the same XML file then I need to clean the current file then after that only dump the new content into the file?
QString xml = doc.toString();
You don't need to clean it. Just open the file in read/write mode and the contents will be truncated.
after i update my xml file. the file content become like below:
-----------------------------------------------
<?xml version='1.0' encoding='UTF-8'?>
<communication OS="3" CMD="03" VER="1.0" >
<parameters>
<parameter MODNAME="iCMDataCard" MODVERS="1.0.1.2" />
</parameters>
</communication>
-1ion>
------------------------------------------------
the file is not able to open is my vs 2005. got one warning message "Inconsistent line endings"
QDomElement sub = item.firstChildElement("parameter");
QString strTemp1 = sub.attribute("MODVERS");
sub.setAttribute("MODVERS", "1.0.1.2");
strTemp1 = sub.attribute("MODVERS");
file.open(QIODevice::ReadWrite);
QTextStream out(&file);
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString strUTF8 = codec->toUnicode(doc.toString().toLocal8Bit());
out << strUTF8;
out << EOF;
file.close();
try this code
Qt Code:
... QDomDocument doc; doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"")); root.setAttribute("OS", 3); root.setAttribute("CMD", "03"); root.setAttribute("VER", 1.0); parameter.setAttribute("MODNAME", "iCMDataCard"); parameter.setAttribute("MODVERS", "1.0.1.2"); parameters.appendChild(parameter); root.appendChild(parameters); doc.appendChild(root); out << doc.toString(); } ...To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Bookmarks