PDA

View Full Version : XML parser problem



Cantora
5th June 2009, 09:32
Hi All,

Below is my xml file content and my code to read it. How to read/write the MODVERS value in parameter tag? and

------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<communication CMD="03" OS="3" VER="1.0">
<parameters>
<parameter MODNAME="testing module" MODVERS="1.0.0.0"/>
</parameters>
</communication>
-------------------------------------------------------------------------------------

QFile file(strXMLPath);
if(!QFile::exists(strXMLPath) || !file.open(QIODevice::ReadWrite) )
{
return;
}
//load the xml content
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
QDomElement item = root.firstChildElement();

while(!item.isNull())
{
QString strTag = item.tagName();
item = item.nextSiblingElement();
}

wysota
5th June 2009, 09:37
Using QDomElement::attribute() and QDomElement::setAttribute()

Cantora
5th June 2009, 09:51
For my current logic.. I am only able to read until <parameters>. Now I still fail to make my code can read until <parameter>

Cantora
5th June 2009, 10:00
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");

Cantora
5th June 2009, 10:17
after I change the MODVERS value how I going to update the XML file?

wysota
5th June 2009, 10:20
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.

Cantora
5th June 2009, 10:52
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();

wysota
5th June 2009, 11:06
You don't need to clean it. Just open the file in read/write mode and the contents will be truncated.

Cantora
5th June 2009, 11:55
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();

spirit
5th June 2009, 12:13
try this code


...
QDomDocument doc;
doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
QDomElement root = doc.createElement("communication");
root.setAttribute("OS", 3);
root.setAttribute("CMD", "03");
root.setAttribute("VER", 1.0);
QDomElement parameters = doc.createElement("parameters");
QDomElement parameter = doc.createElement("parameter");
parameter.setAttribute("MODNAME", "iCMDataCard");
parameter.setAttribute("MODVERS", "1.0.1.2");
parameters.appendChild(parameter);
root.appendChild(parameters);
doc.appendChild(root);

QFile file("settings.xml");
if (file.open(QIODevice::WriteOnly)) {
QTextStream out(&file);
out << doc.toString();
}
...