进步始于交流
收货源于分享

QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写

介绍

帮助文档:QXmlStreamReaderQXmlStreamWriter

除此以外读取时还需要使用QXmlStreamAttributes

QXml-Token标记类型

Constant Value Description
QXmlStreamReader::NoToken 0 The reader has not yet read anything.
QXmlStreamReader::Invalid 1 An error has occurred, reported in error() and errorString().
QXmlStreamReader::StartDocument 2 The reader reports the XML version number in documentVersion(), and the encoding as specified in the XML document in documentEncoding(). If the document is declared standalone, isStandaloneDocument() returns true; otherwise it returns false.
QXmlStreamReader::EndDocument 3 The reader reports the end of the document.
QXmlStreamReader::StartElement 4 The reader reports the start of an element with namespaceUri() and name(). Empty elements are also reported as StartElement, followed directly by EndElement. The convenience function readElementText() can be called to concatenate all content until the corresponding EndElement. Attributes are reported in attributes(), namespace declarations in namespaceDeclarations().
QXmlStreamReader::EndElement 5 The reader reports the end of an element with namespaceUri() and name().
QXmlStreamReader::Characters 6 The reader reports characters in text(). If the characters are all white-space, isWhitespace() returns true. If the characters stem from a CDATA section, isCDATA() returns true.
QXmlStreamReader::Comment 7 The reader reports a comment in text().
QXmlStreamReader::DTD 8 The reader reports a DTD in text(), notation declarations in notationDeclarations(), and entity declarations in entityDeclarations(). Details of the DTD declaration are reported in in dtdName(), dtdPublicId(), and dtdSystemId().
QXmlStreamReader::EntityReference 9 The reader reports an entity reference that could not be resolved. The name of the reference is reported in name(), the replacement text in text().
QXmlStreamReader::ProcessingInstruction 10 The reader reports a processing instruction in processingInstructionTarget() and processingInstructionData().

主要是用StartDocumentEndDocument文档开始结束,StartElementEndElement元素开始结束、Characters特征

范例xml文件

源码请见GitHub:QtCoreExamples

<?xml version="1.0" encoding="UTF-8"?>
<bookmark href="http://qt-project.org/">
    <title>Qt Project</title>
</bookmark>

第一行为StartDocument

bookmark、title为StartElement,/bookmark、/title为EndElement

href为attributes的一项,可以有多项,通过QXmlStreamAttributes::value获取后面的地址内容

Qt Project是title这个element的charcters

写xml

#include <QCoreApplication>
#include <QFile>
#include <QXmlStreamWriter>
int main2(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    QFile file("test.xml");
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QXmlStreamWriter stream(&file);
        stream.setAutoFormatting(true);
        stream.writeStartDocument();
        stream.writeStartElement("bookmark");
        stream.writeAttribute("href", "http://qt-project.org/");
        stream.writeTextElement("title", "Qt Project");
        stream.writeEndElement();
        stream.writeEndDocument();
        file.close();
    }
    return 0;
}

写并不复杂,按顺序写即可,注意区分Attribute和Element,如果是一个小节用StartElement,如果是单行的类似于<title>Qt Project</title>可以直接用writeTextElement。

QXmlStreamWriter只是格式操作,并不提供文件操作,需要利用QFile建立文件并传递指针,也可以提供QString的指针,这样最终的xml信息会赋值到QString中。

读xml

#include <QCoreApplication>
#include <QFile>
#include <QXmlStreamReader>
#include <QDebug>
int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    QFile file("test.xml");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QXmlStreamReader xml(&file);
        while (!xml.atEnd() && !xml.hasError()) {//循环逐行读取
            QXmlStreamReader::TokenType token = xml.readNext();
            if(token == QXmlStreamReader::StartDocument)//文件开始跳过
                continue;
            if(token == QXmlStreamReader::StartElement) {//StartElement类型,主要针对bookmark和title
                if(xml.name() == "bookmark") {//bookmark读取,其下attributes有但只有一个
                    qDebug()<<"StartElement-"<<xml.name();
                    QXmlStreamAttributes attributes = xml.attributes();
                    if(attributes.hasAttribute("href")) {//针对href的attribute做判断
                        qDebug()<<"Attributes-"<<attributes.value("href");//返回值是QStringRef
                    }
                    //多个attributes在这里增加更多的if
                    continue;
                }
                if(xml.name() == "title") {//title
                    xml.readNext();//没有attributes,理应直接进入while,此处偷懒了
                    if(xml.isCharacters()) //可以做Characters判断也可以直接用text
                        qDebug()<<"title-Characters-"<<xml.text();
                }
            }
        }
        if (xml.hasError()) {
            //范例,判断枚举类型,写出错误字符串
            if(xml.error() == QXmlStreamReader::CustomError) {
                //可以直接写出错误字符串
                qDebug()<<"error:" <<xml.errorString();
            }
        }
    }
    return 0;
}
  • 上述代码都没判断EndElement,不建议这样
  • 建议进入每一个StartElement都直接开启一个while循环,直到循环到EndElement,从何保证对一个开头到结尾的完整操作,而不是像上述代码吧title放在和bookmark同级的循环内,并没有标明title包含于bookmark的关系,这样容易导致错误。
  • 注意xml中每一个<>括住的项都作为一个标记,都占用一次readNext,其开头均为name,其后可能有attribute及对应value。
  • 每两个<>XXX<>之间的XXX均作为一个Characters标记,也占用一次readNext,也就是上文中仍有未打印出的Character(“\n??? “换行符和四个空格、纯换行符):因为QXml以”\r”作为换行识别那么在前后两行的<><>之间还会余留一个\n;在title前的缩进也会当做一项Character。因此建议保证完整的包含关系并做好isXXX或者token枚举类型的判断以免被干扰。

其他

除上述xml操作以外,Qt还提供了Qt XML模块,用于更高级的xml操作,提供了高速及使用便利的操作函数(不可兼得呀)

高速的SAX方法读取,QXmlSimpleReader及相关类

使用便捷的DOM方式:QDomDocument及相关类

使用这两个方法,由于是用的非core模块,需要在pro中添加qt += xml

 

赞(0) 打赏
未经允许不得转载:Techie亮博客 » QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏