一、XML文件
<?xml version="1.0" encoding="UTF-8"?>
<Begin><Type name="zhangsan"><sex>boy</sex><school>Chengdu</school><age>18</age><special>handsome</special></Type><Type name="lisi"><sex>boy</sex><school>Xian</school><age>19</age><special>ugly</special></Type>
</Begin>
<?xml version="1.0" encoding="UTF-8"?>版本说明与编号
“Begin”根节点,可以理解为一级目录
”TypeX“是子节点,可以理解为二级目录;”name“是属性,”zhangsan“是属性值
“sex”“school”等是三级节点,可以理解为三级目录;”girl“”“Xian”等是文本;
二、QT中的QDomDocument类
使用时需要添加xml模块。
qt帮助手册简单例子
三、实际代码操作读写操作
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QFile>
#include <QDomDocument>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;private:void my_xml_read();void my_xml_write();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//xml读写my_xml_read();my_xml_write();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::my_xml_read()
{QDomDocument doc("info");QFile file(":/info.xml");if (!file.open(QIODevice::ReadOnly))return;if (!doc.setContent(&file))//将文件与DomDocument关联{file.close();return;}file.close();QDomElement firstElem = doc.documentElement();//获取到了<begin>一级目录qDebug()<<"一级目录<begin>: "<<firstElem.nodeName();QDomNodeList secondList = firstElem.childNodes();//获取全部二级目录<Type1><Type2><Typ3>for (int i = 0; i < secondList.count(); ++i) {QString str = QString(" 第 %1 个二级目录节点:").arg(i+1);qDebug()<<str.toUtf8().data()<<secondList.at(i).nodeName();//将QString转换成char *,使用qDebug输出没有引号QDomElement secondElem = secondList.at(i).toElement();//获取二级目录节点的属性值管理者domElementif(secondElem.hasAttribute("name")){//输出二级目录节点的属性值qDebug()<<" "<<secondElem.nodeName().append("的属性值name为").toUtf8().data()<<secondElem.attribute("name");}else{qDebug()<<" "<<secondElem.nodeName().append("该节点无属性值").toUtf8().data();}QDomNodeList thirdList = secondList.at(i).childNodes();//获取三级目录<具体值></具体值>for(int j = 0;j<thirdList.length();j++ ){QDomElement thirdElement=thirdList.at(j).toElement();QString str_1 = QString(" %1中第 %2 个三级目录节点 %3,他的文本值为 %4:").arg(secondList.at(i).nodeName(),QString::number(j+1),thirdList.at(j).nodeName(),thirdElement.text());qDebug()<<str_1.toUtf8().data();//将QString转换成char *,使用qDebug输出没有引号}}
}void MainWindow::my_xml_write()
{//add instructionQDomDocument doc;QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");//写xml中的第一行说明doc.appendChild(instruction);//add 第一级目录节点QDomElement firstNode = doc.createElement("Begin");doc.appendChild(firstNode);//add 第二级目录节点QDomElement secondNode = doc.createElement("Type");//为二级目录添加属性值QDomAttr secondAttr = doc.createAttribute("MyLover");secondAttr.setValue("lr");secondNode.setAttributeNode(secondAttr);firstNode.appendChild(secondNode);//一级目录下添加二级节点//add 第三级目录节点 添加text文本值QDomElement thirdNode_1 = doc.createElement("sex");secondNode.appendChild(thirdNode_1);QDomText t1 = doc.createTextNode("girl");thirdNode_1.appendChild(t1);QDomElement thirdNode_2 = doc.createElement("school");secondNode.appendChild(thirdNode_2);QDomText t2 = doc.createTextNode("Chengdu");thirdNode_2.appendChild(t2);QDomElement thirdNode_3 = doc.createElement("age");secondNode.appendChild(thirdNode_3);QDomText t3 = doc.createTextNode("28");thirdNode_3.appendChild(t3);QDomElement thirdNode_4 = doc.createElement("special");secondNode.appendChild(thirdNode_4);QDomText t4 = doc.createTextNode("beautiful");thirdNode_4.appendChild(t4);QString fileName = QFileDialog::getSaveFileName(this,"Save xml file","../my_xml","xml(*.xml)");if(fileName.isEmpty()){return;}QFile myFile(fileName);bool ret =myFile.open(QIODevice::WriteOnly|QIODevice::Text);if(!ret){QMessageBox::warning(this,"warning","保存文件失败!");return;}QTextStream stream(&myFile);//使用QTextStream类与文件关联进行保存xml!stream.setCodec("UTF-8");//编码格式doc.save(stream,4);//4代表的是缩进个数myFile.close();QMessageBox::information(this,"save tip","xml文件保存成功!");
}
四、项目运行
与写操作写的信息一致