Editing XML
-
wrote on 17 Jul 2012, 10:59 last edited by
Hi,
I have a XML like this:
@
<rootTag>
<device desc="customDevice1" type="1" id="21"/>
<device desc="customDevice2" type="2" id="22"/>
<device desc="customDevice3" type="3" id="23"/>
<function id="1">Some content function1</function>
<function id="2">Some content function2</function>
<function id="3">Some content function3</function>
</rootTag>
@I would like to modify the existing values of "function id" or any attributes of device. After modifying I need to save the same file.
Please suggest any ideas.
Thanks,
Haney. -
wrote on 17 Jul 2012, 11:19 last edited by
Did you take a look at the tree sets of XML classes that Qt has?
- DOM-based: [[doc:QXmlDomDocument]] and friends
- SAX-based: [[doc:QXmlReader]] and friends
- stream-based: [[doc:QXmlStreamReader]] and [[doc:QXmlStreamWriter]]
-
wrote on 17 Jul 2012, 11:28 last edited by
[quote author="Andre" date="1342523969"]
- DOM-based: [[doc:QXmlDomDocument]] and friends
[/quote]
haney, please have a look at "this example of DOM-based parsing XML":http://www.developer.nokia.com/Community/Wiki/Using_QDomDocument_to_parse_XML using "QDomDocument":http://qt-project.org/doc/qt-4.8/qdomdocument.html.
- DOM-based: [[doc:QXmlDomDocument]] and friends
-
wrote on 18 Jul 2012, 05:04 last edited by
Thanks for your replies.
I am able to use DOM based parsing XML and successfully reading the values. I want help in writing/editing in the same xml.
Eg: I just want to edit/ rewrite function id =1 CONTENT.
I want to add new device say <device desc="customDevice4" type="4" id="24"/>
DOM based parsing is really nice for reading. Please suggest me how to rewrite/modify values.
Code for Reading:
@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);QFile xmlFile(":/sample.xml"); xmlFile.open(QIODevice::ReadOnly); QDomDocument doc; doc.setContent(xmlFile.readAll()); QDomElement root = doc.documentElement(); QDomElement nodeTag = root.firstChildElement("device"); while(!nodeTag.isNull()) { qDebug()<<"attributes of device"<<nodeTag.attribute("desc")<<"type" <<nodeTag.attribute("type") <<"id"<<nodeTag.attribute("id"); nodeTag = nodeTag.nextSiblingElement("device"); }
QDomNodeList nodeList = root.elementsByTagName("function");
for(int i =0;i<nodeList.count();i++)
{
QDomElement el = nodeList.at(i).toElement();qDebug()<<nodeList.at(i).toElement().text(); // prints content qDebug()<<el.attribute("id"); // prints id value
}
xmlFile.close(); return app.exec();
}
@Now that reading is fine.. I need to replace old content with new content. Also I need to modify desc of device, type, id values.
Appreciate your help in this regard.
Thanks,
Haney. -
wrote on 18 Jul 2012, 08:53 last edited by
What have you tried already with regards to modifying values or creating new ones? Did you notice methods like setNodeValue(), appendChild() and setAttribute()?
-
wrote on 18 Jul 2012, 09:34 last edited by
@
el.setAttribute("id", 500);
nodeList.at(i).toElement().setNodeValue("new content");
@I just tried these two lines of code in after line num 26 in above post. I made xmlFile.open(QIODevice::WriteOnly);
but I did not see any new content in my xml file. Do I need to save it in a different way. Please share any working example of editing xml.
Thanks for the favour.
Thanks,
Haney. -
wrote on 18 Jul 2012, 10:00 last edited by
Of course, you just changed the content in the DomDocument. To save the changes to the file you need to write the content of the DomDocument back to the file (overwrite the file).
-
wrote on 18 Jul 2012, 20:27 last edited by
KA510 is right. You can use something like this to overwrite the file:
@
xmlfile.resize(0);
QTextStream stream;
stream.setDevice(&xmlfile);
doc.save(stream,0);
xmlfile.close();
@I'm new to Qt too, so maybe there's a better way to do it.
1/8