Qt写XML文件的步骤
1、基本使用方法
// 头文件 #include <QDebug> #include <QFile> #include <QIODevice> #include <QString> #include <QXmlStreamReader> #include <QXmlStreamAttribute> #include <QXmlStreamAttributes> #define cout qDebug() << "[" << __FILE__ << ":" << __LINE__ << "]:" // 读取xml的函数 void readXmlFile(const QString& filePath) { QFile file(filePath); if( !file.open(QIODevice::ReadOnly | QIODevice::Text ) ) { cout << "file open error!"; return; } QXmlStreamReader xml(&file); while( !xml.atEnd() && !xml.hasError() ) { QXmlStreamReader::TokenType token = xml.readNext(); switch(token) { // 读取头部 case QXmlStreamReader::StartDocument: { cout << xml.documentVersion().toString(); cout << xml.documentEncoding().toString(); }; break; // 读取开头元素和该元素属性 case QXmlStreamReader::StartElement: { cout << xml.name().toString(); QXmlStreamAttributes attrs = xml.attributes(); for( const QXmlStreamAttribute& attr : attrs ) { cout << attr.name().toString(); cout << attr.value().toString(); } }; break; // 读取元素的文本内容 case QXmlStreamReader::Characters: { if( !xml.isWhitespace() ) // 不输出回车换行 { cout << xml.text().toString(); } }; break; // 读取元素结尾 case QXmlStreamReader::EndElement: { cout << xml.name().toString(); }; break; // 文件末尾 case QXmlStreamReader::EndDocument: { cout << QString::fromLocal8Bit("xml读取完毕"); }; break; } } file.close(); } // 函数使用方法 int main(int argc, char * argv[]) { readXmlFile("./template.xml"); system("pause"); // 需要添加头文件,#include <stdlib.h> return 0; }2、进阶用法
按元素块读取数据
// 头文件 #include <QDebug> #include <QFile> #include <QIODevice> #include <QString> #include <QXmlStreamReader> #include <QXmlStreamAttribute> #include <QXmlStreamAttributes> #define cout qDebug() << "[" << __FILE__ << ":" << __LINE__ << "]:" void readElementBlock(QXmlStreamReader& xml, const QString& elementName) { // 输出该元素头信息 cout << xml.name().toString(); QXmlStreamAttributes attrs = xml.attributes(); for( const QXmlStreamAttribute& attr : attrs ) { cout << attr.name().toString(); cout << attr.value().toString(); } while( !xml.atEnd() && !xml.hasError() ) { QXmlStreamReader::TokenType token = xml.readNext(); switch(token) { case QXmlStreamReader::StartElement: { cout << xml.name().toString(); QXmlStreamAttributes childAttrs = xml.attributes(); for( const QXmlStreamAttribute& childAttr : childAttrs ) { cout << childAttr.name().toString(); cout << childAttr.value().toString(); } }; break; case QXmlStreamReader::Characters: { if( !xml.isWhitespace() ) // 不输出回车换行 { cout << xml.text().toString(); } }; break; case QXmlStreamReader::EndElement: { if( xml.name().toString() == elementName ) { cout << xml.name().toString(); return; } cout << xml.name().toString(); }; break; } } } // 读取xml的函数 void readXmlFile(const QString& filePath, const QString& childName) { QFile file(filePath); if( !file.open(QIODevice::ReadOnly | QIODevice::Text ) ) { cout << "file open error!"; return; } QXmlStreamReader xml(&file); while( !xml.atEnd() && !xml.hasError() ) { QXmlStreamReader::TokenType token = xml.readNext(); switch(token) { // 读取头部 case QXmlStreamReader::StartDocument: { cout << xml.documentVersion().toString(); cout << xml.documentEncoding().toString(); }; break; // 读取开头元素和该元素属性 case QXmlStreamReader::StartElement: { // 这里可以加条件,输出你想输出的元素块 if( childName == xml.name().toString() ) { readElementBlock(xml, xml.name().toString()); } }; break; // 文件末尾 case QXmlStreamReader::EndDocument: { cout << QString::fromLocal8Bit("xml读取完毕"); }; break; } } file.close(); } // 函数使用方法 int main(int argc, char * argv[]) { readXmlFile("./template.xml", "node"); // 参数( 文件名, xml文件中的元素名 ) system("pause"); // 需要添加头文件,#include <stdlib.h> return 0; }