Qt在读取xml文件时,如果xml文件中存在大量中文,需要将:
1、源码.cpp文件的编码格式由“UTF-8”改为“GB18030”,按编码重新载入,再改为“GB2312”,按编码保存;
2、在匹配中文数据项时,需要通过QStringLiteral或QString::fromLocal8Bit将将本地(Local 8-Bit)的字符串转为QString类型的字符串。
否则在执行下面“elementsByTagName”代码时,报错:
QDomElement root = doc.documentElement(); //获取根节点
QDomNodeList domesticList = root.elementsByTagName(QStringLiteral("国产车")); //遍历国产车节点
示例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</价格>
</车辆>
</传统燃油车>
</进口车>
</车辆数据库>
示例头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
int ReadXML(); //读取XML
private:
Ui::MainWindow *ui;
};
#endif //MAINWINDOW_H
示例源文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNodeList>
#include <QDebug>
//#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle(QStringLiteral("XML文件解析测试Demo"));
ReadXML(); //读取XML
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::ReadXML() //读取XML
{
// QFile file("D:\\Tests\\Build_Files\\build-ParseXML\\测试XML\\DataRange.xml");
QFile file(QStringLiteral("D:\\Tests\\Build_Files\\build-ParseXML\\测试XML\\Car.xml"));
if (!file.open(QIODevice::ReadOnly)) {
qDebug()<<"Failed to open file for reading";
return -1;
}
// qDebug()<<__FILE__<<__LINE__<<file.fileName()<<file.exists();
QDomDocument doc;
int errLine = -1;
int errColum = -1;
QString strError = "";
if (!doc.setContent(&file, &strError, &errLine, &errColum)) {
file.close();
qDebug()<<"Failed to parse the XML file: "<<strError<<errLine<<errColum;
return -1;
}
// QDomDocument doc;
// QString error = "";
// int row = 0, column = 0;
// if(!doc.setContent(&file, false, &error, &row, &column)){
// qDebug() << "parse file failed:" << row << "---" << column <<":" <<error;
// file.close();
// return -1;
// }
file.close();
QDomElement root = doc.documentElement(); //获取根节点
QDomNodeList domesticList = root.elementsByTagName(QStringLiteral("国产车")); //遍历国产车节点
for (int i = 0; i < domesticList.count(); ++i) {
QDomElement domestic = domesticList.at(i).toElement();
QDomNodeList domesticElectricVehiclesList = domestic.elementsByTagName(QStringLiteral("新能源车")); //遍历国产车下的新能源车节点
for (int j = 0; j < domesticElectricVehiclesList.count(); ++j) {
QDomElement domesticElectricVehicles = domesticElectricVehiclesList.at(j).toElement();
QDomNodeList domesticCarsList = domesticElectricVehicles.elementsByTagName(QStringLiteral("车辆")); //遍历国产车下的新能源车的车辆节点
for (int k = 0; k < domesticCarsList.count(); ++k) {
QDomElement domesticCar = domesticCarsList.at(k).toElement();
QString brand = domesticCar.firstChildElement(QStringLiteral("品牌")).text(); //读取车辆信息
QString model = domesticCar.firstChildElement(QStringLiteral("型号")).text();
QString year = domesticCar.firstChildElement(QStringLiteral("年份")).text();
QString price = domesticCar.firstChildElement(QStringLiteral("价格")).text();
qDebug()<<QStringLiteral("1国产车 - 新能源车 - 车辆信息:");
qDebug()<<QStringLiteral("品牌:")<< brand;
qDebug()<<QStringLiteral("型号:")<< model;
qDebug()<<QStringLiteral("年份:")<< year;
qDebug()<<QStringLiteral("价格:")<< price;
}
}
QDomNodeList domesticConventionalVehiclesList = domestic.elementsByTagName(QStringLiteral("传统燃油车")); //遍历国产车下的传统燃油车节点
for (int j = 0; j < domesticConventionalVehiclesList.count(); ++j) {
QDomElement domesticConventionalVehicles = domesticConventionalVehiclesList.at(j).toElement();
//遍历国产车下的传统燃油车的车辆节点
QDomNodeList domesticCarsList = domesticConventionalVehicles.elementsByTagName(QStringLiteral("车辆"));
for (int k = 0; k < domesticCarsList.count(); ++k) {
QDomElement domesticCar = domesticCarsList.at(k).toElement();
QString brand = domesticCar.firstChildElement(QStringLiteral("品牌")).text(); //读取车辆信息
QString model = domesticCar.firstChildElement(QStringLiteral("型号")).text();
QString year = domesticCar.firstChildElement(QStringLiteral("年份")).text();
QString price = domesticCar.firstChildElement(QStringLiteral("价格")).text();
qDebug()<<QStringLiteral("2国产车 - 传统燃油车 - 车辆信息:");
qDebug()<<QStringLiteral("品牌:")<< brand;
qDebug()<<QStringLiteral("型号:")<< model;
qDebug()<<QStringLiteral("年份:")<< year;
qDebug()<<QStringLiteral("价格:")<< price;
}
}
}
QDomNodeList importedList = root.elementsByTagName(QStringLiteral("进口车")); //遍历进口车节点
for (int i = 0; i < importedList.count(); ++i) {
QDomElement imported = importedList.at(i).toElement();
QDomNodeList importedElectricVehiclesList = imported.elementsByTagName(QStringLiteral("新能源车")); //遍历进口车下的新能源车节点
for (int j = 0; j < importedElectricVehiclesList.count(); ++j) {
QDomElement importedElectricVehicles = importedElectricVehiclesList.at(j).toElement();
QDomNodeList importedCarsList = importedElectricVehicles.elementsByTagName(QStringLiteral("车辆")); //遍历进口车下的新能源车的车辆节点
for (int k = 0; k < importedCarsList.count(); ++k) {
QDomElement importedCar = importedCarsList.at(k).toElement();
QString brand = importedCar.firstChildElement(QStringLiteral("品牌")).text(); //读取车辆信息
QString model = importedCar.firstChildElement(QStringLiteral("型号")).text();
QString year = importedCar.firstChildElement(QStringLiteral("年份")).text();
QString price = importedCar.firstChildElement(QStringLiteral("价格")).text();
qDebug()<<QStringLiteral("3进口车 - 新能源车 - 车辆信息:");
qDebug()<<QStringLiteral("品牌:")<< brand.toLocal8Bit();
qDebug()<<QStringLiteral("型号:")<< model;
qDebug()<<QStringLiteral("年份:")<< year;
qDebug()<<QStringLiteral("价格:")<< price;
}
}
QDomNodeList importedConventionalVehiclesList = imported.elementsByTagName(QStringLiteral("传统燃油车")); //遍历进口车下的传统燃油车节点
for (int j = 0; j < importedConventionalVehiclesList.count(); ++j) {
QDomElement importedConventionalVehicles = importedConventionalVehiclesList.at(j).toElement();
//遍历进口车下的传统燃油车的车辆节点
QDomNodeList importedCarsList = importedConventionalVehicles.elementsByTagName(QStringLiteral("车辆"));
for (int k = 0; k < importedCarsList.count(); ++k) {
QDomElement importedCar = importedCarsList.at(k).toElement();
QString brand = importedCar.firstChildElement(QStringLiteral("品牌")).text(); //读取车辆信息
QString model = importedCar.firstChildElement(QStringLiteral("型号")).text();
QString year = importedCar.firstChildElement(QStringLiteral("年份")).text();
QString price = importedCar.firstChildElement(QStringLiteral("价格")).text();
qDebug()<<QStringLiteral("4进口车 - 传统燃油车 - 车辆信息:");
qDebug()<<QStringLiteral("品牌:")<< brand;
qDebug()<<QStringLiteral("型号:")<< model;
qDebug()<<QStringLiteral("年份:")<< year;
qDebug()<<QStringLiteral("价格:")<< price;
}
}
}
return 0;
}
执行结果:
"1国产车 - 新能源车 - 车辆信息:"
"品牌:" "比亚迪"
"型号:" "汉EV"
"年份:" "2022"
"价格:" "35000"
"1国产车 - 新能源车 - 车辆信息:"
"品牌:" "蔚来"
"型号:" "ES6"
"年份:" "2021"
"价格:" "40000"
"2国产车 - 传统燃油车 - 车辆信息:"
"品牌:" "吉利"
"型号:" "帝豪"
"年份:" "2020"
"价格:" "15000"
"2国产车 - 传统燃油车 - 车辆信息:"
"品牌:" "奇瑞"
"型号:" "艾瑞泽5"
"年份:" "2019"
"价格:" "12000"
"3进口车 - 新能源车 - 车辆信息:"
"品牌:" "\xCC\xD8\xCB\xB9\xC0\xAD"
"型号:" "Model 3"
"年份:" "2021"
"价格:" "50000"
"3进口车 - 新能源车 - 车辆信息:"
"品牌:" "\xB1\xA6\xC2\xED"
"型号:" "i3"
"年份:" "2020"
"价格:" "45000"
"4进口车 - 传统燃油车 - 车辆信息:"
"品牌:" "丰田"
"型号:" "凯美瑞"
"年份:" "2020"
"价格:" "25000"
"4进口车 - 传统燃油车 - 车辆信息:"
"品牌:" "本田"
"型号:" "思域"
"年份:" "2019"
"价格:" "22000"