需要使用到的xml文件如下,车辆数据库下包含两个节点(国产车和进口车,这里面又分新能源车和传统燃油车);
<?xml version="1.0" encoding="UTF-8"?>
<车辆数据库>
<国产车>
<新能源车 >
<车辆>
<品牌>比亚迪</品牌>
<型号>汉EV</型号>
<年份>2022</年份>
<价格>35000</价格>
</车辆>
<车辆>
<品牌>蔚来</品牌>
<型号>ES6</型号>
<年份>2021</年份>
<价格>40000</价格>
</车辆>
</新能源车>
<传统燃油车>
<车辆>
<品牌>吉利</品牌>
<型号>帝豪</型号>
<年份>2020</年份>
<价格>15000</价格>
</车辆>
<车辆>
<品牌>奇瑞</品牌>
<型号>艾瑞泽5</型号>
<年份>2019</年份>
<价格>12000</价格>
</车辆>
</传统燃油车>
</国产车>
<进口车>
<新能源车>
<车辆>
<品牌>特斯拉</品牌>
<型号>Model 3</型号>
<年份>2021</年份>
<价格>50000</价格>
</车辆>
<车辆>
<品牌>宝马</品牌>
<型号>i3</型号>
<年份>2020</年份>
<价格>45000</价格>
</车辆>
</新能源车>
<传统燃油车>
<车辆>
<品牌>丰田</品牌>
<型号>凯美瑞</型号>
<年份>2020</年份>
<价格>25000</价格>
</车辆>
<车辆>
<品牌>本田</品牌>
<型号>思域</型号>
<年份>2019</年份>
<价格>22000</价格>
</车辆>
</传统燃油车>
</进口车>
</车辆数据库>
一、使用QDocument
1、生成上述文件(写文件)
#include <QDomDocument>
#include <QFile>
#include <QTextStream>
int main() {
QDomDocument doc;
// 创建根节点
QDomElement root = doc.createElement("车辆数据库");
doc.appendChild(root);
// 创建国产车节点
QDomElement domestic = doc.createElement("国产车");
root.appendChild(domestic);
// 创建国产车下的新能源车节点
QDomElement domesticElectricVehicles = doc.createElement("新能源车");
domestic.appendChild(domesticElectricVehicles);
// 创建国产车下的新能源车的车辆节点
QDomElement domesticCar1 = doc.createElement("车辆");
domesticElectricVehicles.appendChild(domesticCar1);
// 设置国产车下的新能源车的车辆节点的内容
QDomElement domesticBrand1 = doc.createElement("品牌");
domesticBrand1.appendChild(doc.createTextNode("比亚迪"));
domesticCar1.appendChild(domesticBrand1);
QDomElement domesticModel1 = doc.createElement("型号");
domesticModel1.appendChild(doc.createTextNode("汉EV"));
domesticCar1.appendChild(domesticModel1);
QDomElement domesticYear1 = doc.createElement("年份");
domesticYear1.appendChild(doc.createTextNode("2022"));
domesticCar1.appendChild(domesticYear1);
QDomElement domesticPrice1 = doc.createElement("价格");
domesticPrice1.appendChild(doc.createTextNode("35000"));
domesticCar1.appendChild(domesticPrice1);
// 创建国产车下的传统燃油车节点
QDomElement domesticConventionalVehicles = doc.createElement("传统燃油车");
domestic.appendChild(domesticConventionalVehicles);
// 创建国产车下的传统燃油车的车辆节点
QDomElement domesticCar2 = doc.createElement("车辆");
domesticConventionalVehicles.appendChild(domesticCar2);
// 设置国产车下的传统燃油车的车辆节点的内容
QDomElement domesticBrand2 = doc.createElement("品牌");
domesticBrand2.appendChild(doc.createTextNode("吉利"));
domesticCar2.appendChild(domesticBrand2);
QDomElement domesticModel2 = doc.createElement("型号");
domesticModel2.appendChild(doc.createTextNode("帝豪"));
domesticCar2.appendChild(domesticModel2);
QDomElement domesticYear2 = doc.createElement("年份");
domesticYear2.appendChild(doc.createTextNode("2020"));
domesticCar2.appendChild(domesticYear2);
QDomElement domesticPrice2 = doc.createElement("价格");
domesticPrice2.appendChild(doc.createTextNode("15000"));
domesticCar2.appendChild(domesticPrice2);
// 创建进口车节点
QDomElement imported = doc.createElement("进口车");
root.appendChild(imported);
// 创建进口车下的新能源车节点
QDomElement importedElectricVehicles = doc.createElement("新能源车");
imported.appendChild(importedElectricVehicles);
// 创建进口车下的新能源车的车辆节点
QDomElement importedCar1 = doc.createElement("车辆");
importedElectricVehicles.appendChild(importedCar1);
// 设置进口车下的新能源车的车辆节点的内容
QDomElement importedBrand1 = doc.createElement("品牌");
importedBrand1.appendChild(doc.createTextNode("特斯拉"));
importedCar1.appendChild(importedBrand1);
QDomElement importedModel1 = doc.createElement("型号");
importedModel1.appendChild(doc.createTextNode("Model 3"));
importedCar1.appendChild(importedModel1);
QDomElement importedYear1 = doc.createElement("年份");
importedYear1.appendChild(doc.createTextNode("2021"));
importedCar1.appendChild(importedYear1);
QDomElement importedPrice1 = doc.createElement("价格");
importedPrice1.appendChild(doc.createTextNode("50000"));
importedCar1.appendChild(importedPrice1);
// 创建进口车下的传统燃油车节点
QDomElement importedConventionalVehicles = doc.createElement("传统燃油车");
imported.appendChild(importedConventionalVehicles);
// 创建进口车下的传统燃油车的车辆节点
QDomElement importedCar2 = doc.createElement("车辆");
importedConventionalVehicles.appendChild(importedCar2);
// 设置进口车下的传统燃油车的车辆节点的内容
QDomElement importedBrand2 = doc.createElement("品牌");
importedBrand2.appendChild(doc.createTextNode("丰田"));
importedCar2.appendChild(importedBrand2);
QDomElement importedModel2 = doc.createElement("型号");
importedModel2.appendChild(doc.createTextNode("凯美瑞"));
importedCar2.appendChild(importedModel2);
QDomElement importedYear2 = doc.createElement("年份");
importedYear2.appendChild(doc.createTextNode("2020"));
importedCar2.appendChild(importedYear2);
QDomElement importedPrice2 = doc.createElement("价格");
importedPrice2.appendChild(doc.createTextNode("25000"));
importedCar2.appendChild(importedPrice2);
// 将XML内容写入文件
QFile file("vehicles_zh.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "Failed to open file for writing";
return -1;
}
QTextStream out(&file);
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; // 添加XML声明行
doc.save(out, 4); // 4是缩进空格数
file.close();
return 0;
}
2、读文件
#include <QDomDocument>
#include <QFile>
#include <QDebug>
int main() {
QFile file("vehicles_zh.xml");
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open file for reading";
return -1;
}
QDomDocument doc;
if (!doc.setContent(&file)) {
file.close();
qWarning() << "Failed to parse the XML file";
return -1;
}
file.close();
// 获取根节点
QDomElement root = doc.documentElement();
// 遍历国产车节点
QDomNodeList domesticList = root.elementsByTagName("国产车");
for (int i = 0; i < domesticList.count(); ++i) {
QDomElement domestic = domesticList.at(i).toElement();
// 遍历国产车下的新能源车节点
QDomNodeList domesticElectricVehiclesList = domestic.elementsByTagName("新能源车");
for (int j = 0; j < domesticElectricVehiclesList.count(); ++j) {
QDomElement domesticElectricVehicles = domesticElectricVehiclesList.at(j).toElement();
// 遍历国产车下的新能源车的车辆节点
QDomNodeList domesticCarsList = domesticElectricVehicles.elementsByTagName("车辆");
for (int k = 0; k < domesticCarsList.count(); ++k) {
QDomElement domesticCar = domesticCarsList.at(k).toElement();
// 读取车辆信息
QString brand = domesticCar.firstChildElement("品牌").text();
QString model = domesticCar.firstChildElement("型号").text();
QString year = domesticCar.firstChildElement("年份").text();
QString price = domesticCar.firstChildElement("价格").text();
qDebug() << "国产车 - 新能源车 - 车辆信息:";
qDebug() << "品牌:" << brand;
qDebug() << "型号:" << model;
qDebug() << "年份:" << year;
qDebug() << "价格:" << price;
}
}
// 遍历国产车下的传统燃油车节点
QDomNodeList domesticConventionalVehiclesList = domestic.elementsByTagName("传统燃油车");
for (int j = 0; j < domesticConventionalVehiclesList.count(); ++j) {
QDomElement domesticConventionalVehicles = domesticConventionalVehiclesList.at(j).toElement();
// 遍历国产车下的传统燃油车的车辆节点
QDomNodeList domesticCarsList = domesticConventionalVehicles.elementsByTagName("车辆");
for (int k = 0; k < domesticCarsList.count(); ++k) {
QDomElement domesticCar = domesticCarsList.at(k).toElement();
// 读取车辆信息
QString brand = domesticCar.firstChildElement("品牌").text();
QString model = domesticCar.firstChildElement("型号").text();
QString year = domesticCar.firstChildElement("年份").text();
QString price = domesticCar.firstChildElement("价格").text();
qDebug() << "国产车 - 传统燃油车 - 车辆信息:";
qDebug() << "品牌:" << brand;
qDebug() << "型号:" << model;
qDebug() << "年份:" << year;
qDebug() << "价格:" << price;
}
}
}
// 遍历进口车节点
QDomNodeList importedList = root.elementsByTagName("进口车");
for (int i = 0; i < importedList.count(); ++i) {
QDomElement imported = importedList.at(i).toElement();
// 遍历进口车下的新能源车节点
QDomNodeList importedElectricVehiclesList = imported.elementsByTagName("新能源车");
for (int j = 0; j < importedElectricVehiclesList.count(); ++j) {
QDomElement importedElectricVehicles = importedElectricVehiclesList.at(j).toElement();
// 遍历进口车下的新能源车的车辆节点
QDomNodeList importedCarsList = importedElectricVehicles.elementsByTagName("车辆");
for (int k = 0; k < importedCarsList.count(); ++k) {
QDomElement importedCar = importedCarsList.at(k).toElement();
// 读取车辆信息
QString brand = importedCar.firstChildElement("品牌").text();
QString model = importedCar.firstChildElement("型号").text();
QString year = importedCar.firstChildElement("年份").text();
QString price = importedCar.firstChildElement("价格").text();
qDebug() << "进口车 - 新能源车 - 车辆信息:";
qDebug() << "品牌:" << brand;
qDebug() << "型号:" << model;
qDebug() << "年份:" << year;
qDebug() << "价格:" << price;
}
}
// 遍历进口车下的传统燃油车节点
QDomNodeList importedConventionalVehiclesList = imported.elementsByTagName("传统燃油车");
for (int j = 0; j < importedConventionalVehiclesList.count(); ++j) {
QDomElement importedConventionalVehicles = importedConventionalVehiclesList.at(j).toElement();
// 遍历进口车下的传统燃油车的车辆节点
QDomNodeList importedCarsList = importedConventionalVehicles.elementsByTagName("车辆");
for (int k = 0; k < importedCarsList.count(); ++k) {
QDomElement importedCar = importedCarsList.at(k).toElement();
// 读取车辆信息
QString brand = importedCar.firstChildElement("品牌").text();
QString model = importedCar.firstChildElement("型号").text();
QString year = importedCar.firstChildElement("年份").text();
QString price = importedCar.firstChildElement("价格").text();
qDebug() << "进口车 - 传统燃油车 - 车辆信息:";
qDebug() << "品牌:" << brand;
qDebug() << "型号:" << model;
qDebug() << "年份:" << year;
qDebug() << "价格:" << price;
}
}
}
return 0;
}
3、删除某个节点(例如删除进口车的本田思域)
#include <QDomDocument>
#include <QFile>
#include <QDebug>
int main() {
QFile file("vehicles_zh.xml");
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open file for reading";
return -1;
}
QDomDocument doc;
if (!doc.setContent(&file)) {
file.close();
qWarning() << "Failed to parse the XML file";
return -1;
}
file.close();
// 获取根节点
QDomElement root = doc.documentElement();
// 遍历进口车节点
QDomNodeList importedList = root.elementsByTagName("进口车");
for (int i = 0; i < importedList.count(); ++i) {
QDomElement imported = importedList.at(i).toElement();
// 遍历进口车下的传统燃油车节点
QDomNodeList importedConventionalVehiclesList = imported.elementsByTagName("传统燃油车");
for (int j = 0; j < importedConventionalVehiclesList.count(); ++j) {
QDomElement importedConventionalVehicles = importedConventionalVehiclesList.at(j).toElement();
// 遍历进口车下的传统燃油车的车辆节点
QDomNodeList importedCarsList = importedConventionalVehicles.elementsByTagName("车辆");
for (int k = 0; k < importedCarsList.count(); ++k) {
QDomElement importedCar = importedCarsList.at(k).toElement();
// 读取车辆信息
QString brand = importedCar.firstChildElement("品牌").text();
QString model = importedCar.firstChildElement("型号").text();
// 检查是否为"本田思域"
if (brand == "本田" && model == "思域") {
// 删除节点
importedConventionalVehicles.removeChild(importedCar);
}
}
}
}
// 将修改后的XML内容写入文件
QFile outputFile("vehicles_zh_modified.xml");
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "Failed to open file for writing";
return -1;
}
QTextStream out(&outputFile);
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; // 添加XML声明行
doc.save(out, 4); // 4是缩进空格数
outputFile.close();
return 0;
}
4、修改某个节点信息(例如把本田思域改成本田大奔驰)
#include <QDomDocument>
#include <QFile>
#include <QDebug>
int main() {
QFile file("vehicles_zh.xml");
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open file for reading";
return -1;
}
QDomDocument doc;
if (!doc.setContent(&file)) {
file.close();
qWarning() << "Failed to parse the XML file";
return -1;
}
file.close();
// 获取根节点
QDomElement root = doc.documentElement();
// 遍历进口车节点
QDomNodeList importedList = root.elementsByTagName("进口车");
for (int i = 0; i < importedList.count(); ++i) {
QDomElement imported = importedList.at(i).toElement();
// 遍历进口车下的传统燃油车节点
QDomNodeList importedConventionalVehiclesList = imported.elementsByTagName("传统燃油车");
for (int j = 0; j < importedConventionalVehiclesList.count(); ++j) {
QDomElement importedConventionalVehicles = importedConventionalVehiclesList.at(j).toElement();
// 遍历进口车下的传统燃油车的车辆节点
QDomNodeList importedCarsList = importedConventionalVehicles.elementsByTagName("车辆");
for (int k = 0; k < importedCarsList.count(); ++k) {
QDomElement importedCar = importedCarsList.at(k).toElement();
// 读取车辆信息
QString brand = importedCar.firstChildElement("品牌").text();
QString model = importedCar.firstChildElement("型号").text();
// 检查是否为"本田思域"
if (brand == "本田" && model == "思域") {
// 修改节点信息
QDomElement modelElement = importedCar.firstChildElement("型号");
modelElement.setNodeValue("大奔驰");
}
}
}
}
// 将修改后的XML内容写入文件
QFile outputFile("vehicles_zh_modified.xml");
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "Failed to open file for writing";
return -1;
}
QTextStream out(&outputFile);
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; // 添加XML声明行
doc.save(out, 4); // 4是缩进空格数
outputFile.close();
return 0;
}