xml文件格式:
<?xml version="1.0"?>
<tree id="0">
<item id="1" open="1" text="一级" im0="leaf.gif" im1="folderOpen.gif" im2="folderClosed.gif" >
<item id="5" select="1" text="二级" im0="leaf.gif" im1="folderOpen.gif" im2="folderClosed.gif" ></item>
<item id="6" text="空泡" im0="leaf.gif" im1="folderOpen.gif" im2="folderClosed.gif" ></item>
</item>
</tree>
头文件:
#include <QFile>
#include <QString>
#include <QDomDocument>
#include <QtXml>
#include <QXmlStreamReader>
示例代码:
//DOM方式查找在XML文件二级目录中是否有sItemName节点,有返回1,无返回0,错误返回-1;
//sXmlFilePath:xml完整路径打的文件名(c:\doc\ss.xml)
int CAppletGenerator::searchTaskMenuTreeItem(QString sXmlFilePath,QString sItemName))
{
QFile file(sXmlFilePath);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
return -1;
}
QDomDocument domDocument;
if (!domDocument.setContent(&file, true)) {
file.close();
return -1;
}
QDomElement tree = domDocument.documentElement();
QDomElement firstItem = tree.firstChildElement();
//把全部二级item存放在list中
QDomNodeList secondItemlist = firstItem.childNodes();
for(int i = 0;i < secondItemlist.count();i++) {
QDomNode node = secondItemlist.at(i);
QString textname = node.toElement().attribute("text");
if (textname == sItemName) {
return 1;
}
}//end for
return 0;
}