#include <Qt/QFile.h>
#include <Qt/QDom.h>
#include <iostream>
void showNodeInfo(const QDomNode& node)
{
std::cout<<"node name:"<<node.nodeName().toStdString()<<std::endl;
QDomNode subnode = node.firstChild();
std::cout<<" node id:"<<subnode.nodeName().toStdString()<<std::endl;
std::cout<<" node id value:"<<subnode.toElement().text().toStdString()<<std::endl;
subnode = subnode.nextSibling();
std::cout<<" node name:"<<subnode.nodeName().toStdString()<<std::endl;
std::cout<<" node name value:"<<subnode.toElement().text().toStdString()<<std::endl;
subnode = subnode.nextSibling();
std::cout<<" node prefix:"<<subnode.nodeName().toStdString()<<std::endl;
std::cout<<" node prefix value:"<<subnode.toElement().text().toStdString()<<std::endl;
}
int main(int argc, char *argv[])
{
QDomDocument doc;
QFile xmlfile("config.xml");
if(!xmlfile.open(QIODevice::ReadOnly))return false;
if(doc.setContent(&xmlfile))
{
QDomElement root = doc.documentElement();
QDomNode node = root.firstChild();
for(;node.isNull()!=true;node=node.nextSibling())
showNodeInfo(node);
}
system("pause");
return 1;
}
基本步骤:
1.使用QFile载入文件
2.给QDomDocument设置文件内容
3.从QDomDocument获取文档根节点
4.遍历节点并作处理