2009年4月28日 星期二

透過qt寫入與讀取xml檔案

必需要注意的是,在每一個tag均不可以有空白存在,若有空白存在,雖然寫入不會出現問題,但是,在讀取時會出現錯誤,而xml檔案的好處就是可以儲存使用者的設定檔

寫入的範例程式
main.cpp

#include <qapplication.h>
#include <qdom.h>
#include <qfile.h>

int main(int argc,char **argv)
{
// 不顯示GUI
// renyang - 若使用這一行的話, 則不在X11也是可以執行,不會連到X服務器
QApplication app(argc,argv,false);

// 在程式中建立一個叫"IhuConfigXML"的xml檔
QDomDocument doc("IhuConfigXML");
// 建立一個在doc中的成員
QDomElement root = doc.createElement("renyang");
// 把root設定為doc目前的最後一個child
doc.appendChild(root);

// 建立一個在doc中的成員
QDomElement general = doc.createElement("general");

// 設定屬性
general.setAttribute("myName","fangrenyang");
general.setAttribute("height","168cm");
general.setAttribute("weight","70kg");

QDomElement family = doc.createElement("family");

family.setAttribute("grandemother",true);
family.setAttribute("father",true);
family.setAttribute("mother",true);
family.setAttribute("old_brother",true);
family.setAttribute("little_brother",true);

QDomElement education = doc.createElement("education");

education.setAttribute("primary_school",true);
education.setAttribute("secondary_school",true);
education.setAttribute("high_school",true);
education.setAttribute("university",true);
education.setAttribute("graduate_school",true);

// 設定general, family, education均為root的child
// 架構圖為
// doc--root--general
// --family
// --education
root.appendChild(general);
root.appendChild(family);
root.appendChild(education);

// 開啟檔案
QFile confFile("confFile.xml");
// 限定開啟檔案的權限
if (confFile.open(IO_WriteOnly))
{
// 建立一個QTextStream, 提供了基本的讀寫的功能
QTextStream ts(&confFile);
ts << doc.toString();
qDebug("create xml success!!");
confFile.close();
}
else
{
qDebug(QString("can't write config file %1 (%2)").arg("confFile.xml").arg(confFile.errorString()));
}

return 0;
}


讀取的範例程式
main.cpp
#include <qapplication.h>
#include <qfile.h>
#include <qdom.h>
#include <qstring.h>

int main(int argc,char **argv)
{
QApplication app(argc,argv,false);

// 用來記錄是否讀取xml檔案成功
bool ret = false;

QDomDocument doc("IhuConfigXML");

QString fileName("confFile.xml");

QFile confFile(fileName);

if (confFile.open(IO_ReadOnly))
{
// This function reads the XML document from the IO device dev
// 若xml內的tag有空白的話,那麼此時會出現錯誤
if (doc.setContent(&confFile))
{
// 設定文件標頭為一開始的最上層tag
QDomElement root = doc.documentElement();
if (root.tagName() == "renyang")
{
// 取得第一個child tage
QDomNode n = root.firstChild();
while (!n.isNull())
{
QDomElement e = n.toElement();
if (!e.isNull())
{
QString tagName = e.tagName();
if (tagName == "general")
{
qDebug(QString("myName: %1").arg(e.attribute("myName",QString(""))));
}
else if (tagName == "family")
{
// 這裡會找不到grandemother, 因為實際檔案中grandmother多打了一個e, 找不到就會回傳後面的預設值
qDebug(QString("grandmother: %1").arg(e.attribute("grandmother","99")));
}
else if (tagName == "education")
{
qDebug(QString("primary_school: %1").arg(e.attribute("primary_school","99")));
}
else
{
// do nothing
}
}
// 下一個tag
n = n.nextSibling();
}
ret = true;
}
else
{
qWarning(QString("Error: %1 is not a valid config file").arg("confFile.xml"));
}
confFile.close();
}
else
{
qWarning(QString("Error: error occurred while parsing %1").arg("confFile.xml"));
}
}
else
{
qWarning(QString("Error: can't read config file %1 (%2)").arg("confFile.xml").arg(confFile.errorString()));
}
return ret;
}

沒有留言: