sss93 0 Newbie Poster

Ive got a basic Qt console application whcih write and then reads back an xml document.I have compiled and ran this without any compilation errors, however for some reason it keeps returning an error code and then wipes clean the actual xml document ??? Can someone please explain why it keeps on returning a failed attempt at reading.

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //Write xml
    QDomDocument doc;

    QDomElement root = doc.createElement("Orders");

    doc.appendChild(root);

    //Add nodes

    for(int i =0; i <5  ; i++){
        QDomElement order = doc.createElement("order");
        order.setAttribute("Oid" , i+1);
        order.setAttribute("total" ,floor(i*3)/4);
        root.appendChild(order);
        for(int x =0; x < 4; x++){
            QDomElement item = doc.createElement("item");
            item.setAttribute("id",floor(i*3)/4);
            item.setAttribute("Cost", 0.50);
            order.appendChild(item);
        }
    }

    //Write to a file
    QFile file("xmlDoc.xml");
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        qDebug() << "Failed to open for write !!";
        return -1;
    }else{
        QTextStream stream(&file);
        stream << doc.toString();
        file.close();
    }

    int stat = readBackXML();
    qDebug() << stat;
    return a.exec();
}

int readBackXML(){

    QDomDocument doc;
    QFile file("C:/Qt/qtcreator-2.5.2/Projects/WriteXml-build-desktop-Qt_4_8_3__4_8_3__Release/xmlDoc.xml");
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug() << "Failed top open file ...";
        return -1;
    }else{
        if (!doc.setContent(&file)) //This is problem line, keeps returning -2 ???
        {
            qDebug() << "Failed to open file ...";
            return -2;
        }
            file.close();
    }

    // Get elements
    QDomElement first = doc.firstChildElement();
    retElements(first,"order", "Oid" );
    return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.