BlackAngelx
New Member
Im trying to make a basic XML read and write Qt console application. And when I do a if statment to determine whether the loading of content was succes it returns with an error code and then wipes clean the pre-existing xml document.The build process is fine and works but it seems to not like the file Im trying to load in (btw main function at bottom) ??\[code\] #include <QCoreApplication> #include <QtCore> #include <QtXml> #include <QDebug> void retElements(QDomElement root, QString tag, QString att){ QDomNodeList items = root.elementsByTagName(tag); qDebug() << "Total items:" << items.count(); for(int i=0; i < items.count();i ++) { QDomNode itemNode = items.at(i); //Convert to element if(itemNode.isElement()){ QDomElement itemEle = itemNode.toElement(); qDebug() << itemEle.attribute(att); } }} 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 to open file ..."; return -1; }else{ if (!doc.setContent(&file)) // Problem line is here it keeps returning -2 ?? { qDebug() << "Failed to open file ..."; return -2; } file.close(); } // Get elements QDomElement first = doc.firstChildElement(); retElements(first,"order", "Oid" ); return 0;} 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(); //Start reading process qDebug() << stat; return a.exec();}\[/code\]