读取XML
1).读取根节点:QDomElement root = doc.documentElement();
2).读取第一个子节点:QDomNode node = root.firstChild();
3).读取下一个子节点:node = node.nextSibling();
4).匹配结点标记:node.toElement().tagName() == "note"
5).读取节点文本:no = childNode.toText().data();
6)可以通过doc.childNodes()获得doc的所有的子节点列表QDomNodeList
---------------------
XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!--leave for a good return-->
<?xml-stylesheet type="text/css" href="style.css"?>
<library Version="2.1">
<book id="1" time="2013/6/13">
<title>C++ primer</title>
<author>Stanley Lippman</author>
</book>
<book id="2" time="2007/5/25">
<title>Thinking in Java</title>
<author>Bruce Eckel</author>
</book>
</library>
#include "mainwindow.h"
#include <QApplication>
#include <QDomDocument>
#include <QString>
#include <QFile>
#include <QDebug>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//打开或创建文件
QFile file("test.xml");
if(!file.open(QFile::ReadOnly))
return 0;
QDomDocument doc;
//设置wangfei1.xml到文档
if(!doc.setContent(&file))
{
file.close();
return 0;
}
file.close();
//返回根节点
QDomElement root=doc.documentElement();
qDebug()<<root.nodeName();
//如果xml根元素有属性(Version)将输出,Vinsion是用户自定义的属性,没有继续执行下一条命令
if (root.hasAttribute("Version")) // 属性
qDebug() << root.attribute("Version");
/**********根元素之上(XML 声明、注释等)**********/
QDomNode node = root.previousSibling();
while (!node.isNull())
{
switch (node.nodeType())
{
case QDomNode::ProcessingInstructionNode :
{
QDomProcessingInstruction instruction = node.toProcessingInstruction();
//输出处理指令,是用户自定义的,比如字符串“name”对应处理指令得到名字,这个命令是用户写的
qDebug() << instruction.target() << instruction.data();
if (QString::compare(instruction.target(), "xml") == 0) // 开始文档(XML 声明)
{
//cout<<"处理命令xml"<<endl;
// ...
}
else if (QString::compare(instruction.target(), "xml-stylesheet") == 0) // 处理指令
{
//cout<<"处理命令xml-stylesheet"<<endl;
// ...
}
break;
}
case QDomNode::CommentNode :
{
QDomComment comment = node.toComment();
qDebug() << comment.data();
break;
}
default:
break;
}
node = node.previousSibling();
}
qDebug() << "************************************previousSibling*******************************";
//获得第一个子节点
node=root.firstChild();
while(!node.isNull()) //如果节点不空
{
if(node.isElement()) //如果节点是元素
{
//转换为元素
QDomElement element=node.toElement();
if (!element.isNull())// 节点的确是一个元素
{
//输出第一个节点,包括第一个节点的属性,这个属性需要指定属性值,才能输出,如果没有输出空
qDebug()<<element.tagName()<<" "<<element.attribute("id")<<" "<<element.attribute("time");
QDomNodeList list=element.childNodes();
for(int i=0;i<list.count();++i)
{
QDomNode n=list.at(i);
//node = list.at(i);
if(node.isElement())
{
qDebug()<<n.nodeName()<<":"<<n.toElement().text();
element = n.toElement();
//qDebug()<<element.nodeName()<<":"<<element.toElement().text();
if (QString::compare(element.tagName(), QStringLiteral("作者")) == 0)
{
// ...处理命令
//cout<< "处理命令作者"<<endl;
}
else if (QString::compare(element.tagName(), QStringLiteral("时间")) == 0)
{
//cout<<"处理命令时间"<<endl;
// ...处理命令
}
else if (QString::compare(element.tagName(), QStringLiteral("个人说明")) == 0)
{
//cout<<"处理命令个人说明"<<endl;
// ...处理命令
}
}
qDebug() << "************************************node list*******************************";
}
}
}
//下一个兄弟节点
node=node.nextSibling();
qDebug() << "************************************next node*******************************";
}
return a.exec();
}
输出结果
"library"
"2.1"
"xml-stylesheet" "type="text/css" href="style.css""
"leave for a good return"
"xml" "version='1.0' encoding='UTF-8'"
************************************previousSibling*******************************
"book" "1" "2013/6/13"
"title" : "C++ primer"
************************************node list*******************************
"author" : "Stanley Lippman"
************************************node list*******************************
************************************next node*******************************
"book" "2" "2007/5/25"
"title" : "Thinking in Java"
************************************node list*******************************
"author" : "Bruce Eckel"
************************************node list*******************************
************************************next node*******************************
对于扩展、删除XML数据文件,详见如下文章
https://blog.csdn.net/wf19930209/article/details/77929770
https://blog.csdn.net/yuanzhangmei1/article/details/7827659