/*******
********
********
********代码创建xml
**********/
1:在项目文件中添加xml模块
如:QT+=xml
2:创建一个QFile对象用于打开xml文件
实例:
QFile file("D:/luohuiqing/qtxml.xml"); //相对路径、绝对路径、资源路径都可以
if(!file.open(QFile::WriteOnly|QFile::Truncate)) //可以用QIODevice,Truncate表示清空原来的内容
{
qDebug()<<"打开失败!";
return;
}
3:写入xml头部
3.1:创建一个QDomDocument对象和QDomProcessingInstruction对象
实例:
QDomDocument doc;
QDomProcessingInstruction instruction;//添加预处理命令
3.2:指定xml版本以及编码和创建文档对象
实例:
instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF- 8\"");
doc.appendChild(instruction);
3.3:添加根节点
QDomElement root=doc.createElement("Human");//human为我们要创建的节点名
doc.appendChild(root);
3.4:添加一个子节点
QDomElement china=doc.createElement("china");
root.appendChild(china);
3.5:创建属性
QDomAttr Pname=doc.createAttribute("name");
3.6:设置属性的值
Pname.setNodeValue("zhonguo");
3.7:将属性和节点关联起来
china.setAttributeNode(Pname);
3.8:将新建的节点添加到已有节点下
root.appendChild(china);
3.9:最后保存我们写入的内容,并且将打开的文件关闭
QTextStream stream(&file);
doc.save(stream,4);
file.close();
/******读取xml文档中的内容*******/
1:打开或则创建xml文档
QDir dir("D:/luohuiqing/QMainWindow");
if (!dir.exists())
{
dir.mkpath("D:/luohuiqing/QMainWindow");
}
QFile file("D:/luohuiqing/QMainWindow/XML.xml"); //相对路径、绝对路径、资源路径都行
if (!file.exists())
workThread::CreateXML();
if(!file.open(QFile::ReadOnly))
return;
2:创建一个QDomDocument对象用来与xml文件关联,关联成功或则失败都要关闭文件,避免浪费资源
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
3:获取xml文档中的根节点,借用根节点来递归遍历内容
QDomElement root=doc.documentElement();
4:通过根节点来获取第一个子节点
QDomNode node = root.firstChild();
5:使用QDomNamedNodeMap类型的对象来保存当前节点中所有的属性和值
QDomNamedNodeMap map = node.attributes();
6:通过下标引用来获取节点当中的某一项属性和值,并将获取的属性用QDomNode来保存
QDomNode debug = map.item(i);
7:获取前面我们保存到的属性节点中的属性值
QDomAttr attr = debug.toAttr();
8:获取前面我们得到的节点中某个属性的属性名
attr.name().toStdString() == "name"
9:获取前面我们得到的属性节点中的属性值,value成员函数的返回值是属性的值
attr.value()
10:因为获取的子节点是QDomNode类型,为了接下来能够递归调用,需要将它转换为
根节点类型
QDomElement element = node.toElement();
11:接下来就可以开始递归调用了,每次调用我们传递的都是一个根节点类型QDomElement(其实传递的都是
子节点)
ReadNeed(element);
12:获取兄弟节点
node=node.nextSibling();
12:注意我们应该将读取xml文件的操作单独写一个函数
void Widget::ReadXml()
{
//打开或创建文件
QDir dir("D:/luohuiqing/QMainWindow");
if (!dir.exists())
{
dir.mkpath("D:/luohuiqing/QMainWindow");
}
QFile file("D:/luohuiqing/QMainWindow/XML.xml"); //相对路径、绝对路径、资源路径都行
if (!file.exists())
workThread::CreateXML();
if(!file.open(QFile::ReadOnly))
return;
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root=doc.documentElement(); //返回根节点
ReadNeed(root,0);
}
void Widget::ReadNeed(QDomElement root, int partent)
{
static int num = 0;
//得到xml文件中的第一个子节点
QDomNode node = root.firstChild();
//假如得到的子节点为空,则结束
while (!node.isNull())
{
if(node.hasAttributes())
{
num++;
if (partent == 0)
item[num] = new QTreeWidgetItem(ui->treeWidget);
else
item[num] = new QTreeWidgetItem(item[partent]);
item[num] ->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
QDomNamedNodeMap map = node.attributes();
for(int i = 0 ; i < map.length() ; ++i)
{
QDomNode debug = map.item(i);
QDomAttr attr = debug.toAttr();
if (attr.name().toStdString() == "name")
item[num] ->setText(0,attr.value());
if (attr.name().toStdString() == "state")
{
if (attr.value() == "0")
item[num] ->setCheckState(0,Qt::Unchecked);
if (attr.value() == "1")
item[num] ->setCheckState(0,Qt::Checked);
if (attr.value() == "2")
item[num] ->setCheckState(0,Qt::PartiallyChecked);
}
if (attr.name().toStdString() == "min")
item[num] ->setText(1,attr.value());
if (attr.name().toStdString() == "max")
item[num] ->setText(2,attr.value());
if (attr.name().toStdString() == "other")
item[num] ->setText(3,attr.value());
}
QDomElement element = node.toElement();
ReadNeed(element,num);
}
node=node.nextSibling();
}
}