PDA

View Full Version : QDomDocumen save method doesn't work!



xiongxiongchuan
1st June 2010, 09:28
i want to create a file and write some xml code in it use the below code like this ,but it don't save anything ,anyone could help me:confused::confused::confused:

QString xmlAddress=projectAddress;
xmlAddress+="//";
xmlAddress+=projectName;
xmlAddress+=".drdp";//项目的后缀名为.drdp


QDomDocument doc;
QDomElement project=doc.createElement("Project");
QDomElement projectInformation=doc.createElement("ProjectInformation");
QDomElement createTime=doc.createElement("CreateTime");
QDomElement name=doc.createElement("Name");
QDomElement author=doc.createElement("Author");
QDomElement description=doc.createElement("Description");
QDomElement unit=doc.createElement("Unit");
QDomElement type=doc.createElement("Type");


QDomElement files=doc.createElement("Files");
QDomElement chart2d=doc.createElement("Chart2D");
QDomElement chart3d=doc.createElement("Chart3D");
QDomElement data=doc.createElement("Data");

QDomElement databases=doc.createElement("Databases");


doc.appendChild(project);


project.appendChild(projectInformation);
project.appendChild(files);
project.appendChild(databases);

projectInformation.appendChild(createTime);
projectInformation.appendChild(name);
projectInformation.appendChild(author);
projectInformation.appendChild(description);
projectInformation.appendChild(unit);
projectInformation.appendChild(type);


QDomText txtCreateTime=doc.createTextNode(projectCreateTime .toString(Qt::DateFormat::ISODate));
QDomText txtAuthor=doc.createTextNode(projectAuthor);
QDomText txtName=doc.createTextNode(projectName);
QDomText txtDescription=doc.createTextNode(projectDescripti on);
QDomText txtType=doc.createTextNode("TextType");
QDomText txtUnit=doc.createTextNode("TextUnit");

createTime.appendChild(txtCreateTime);
name.appendChild(txtName);
author.appendChild(txtAuthor);
description.appendChild(txtDescription);
unit.appendChild(txtUnit);
type.appendChild(txtType);
QDomNode xmlNode = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");

doc.insertBefore(xmlNode, doc.firstChild());






QTextStream out(&xmlAddress);





doc.save(out,4);

Lykurg
1st June 2010, 09:48
Well, it is perfectly saved, but not where you expect it! It is saved in your QString xmlAddress. If you want to save it to a file, specified by xmlAddress, you have to use QFile first!


And did you noticed, that you have used the wrong tags to indicate that your text is c++ code? Please use the [code] tags next time, which makes your code much more readable.

xiongxiongchuan
1st June 2010, 11:44
thank you i have correct this probrem


Well, it is perfectly saved, but not where you expect it! It is saved in your QString xmlAddress. If you want to save it to a file, specified by xmlAddress, you have to use QFile first!


And did you noticed, that you have used the wrong tags to indicate that your text is c++ code? Please use the [code] tags next time, which makes your code much more readable.