QT 中QDomDocument::setContent问题请教
QString xmlPath="E:\\12.xml";
QFile file(xmlPath);
if (!file.open(QIODevice::ReadOnly| QIODevice::Text))
{
//return 0;
}
QDomDocument doc;
QString errorStr;
int errorLine, errorCol;
if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorCol))//此句会出错
{
file.close();
}
QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
cout << qPrintable(e.tagName()) << endl; // the node really is an element.
}
n = n.nextSibling();
}
// Here we append a new element to the end of the document
QDomElement elem = doc.createElement("img");
elem.setAttribute("src", "myimage.png");
docElem.appendChild(elem);