tags:

views:

51

answers:

1

Hi everybody,

I am working on a small programm written in C++ using the QT Framework (v. 4.7).

The task I want to accomplish is opening an existing xml-document, add a node within its structure and save the document to the file I read it from before. I have tried this using something like that:

QFile xmlIn(AFileName);
QDomDocument doc("report_1");
if (xmlIn.open(QIODevice::ReadOnly)) {
    if (doc.setContent(&xmlIn)) {
        QDomElement docElem = doc.documentElement();

        QDomNode n = docElem.firstChild();
        // Do something with the element here
        n = n.nextSibling();
    }
}
// Code for saving the edited QDomDocument comes here

But I have the strong feeling that I am overseeing an essential part here, because this seems to be a task that is very common and normally the QT Lib should provide everything one could want :D

Please enlighten me :)

+1  A: 

Use a "QXmlStreamWriter"...

QXmlStreamWriter    lStreamWriter;

if(lProjectFile.open(QIODevice::WriteOnly|QFile::Text))
{
    lStreamWriter.setDevice(&lProjectFile);
    lStreamWriter.writeStartDocument();
    lStreamWriter.writeStartElement("element");
    lStreamWriter.writeAttribute("attribute","value"); 
    lStreamWriter.writeEndElement();
    lStreamWriter.writeEndDocument();
    lProjectFile.close();
}
Arun