公有函数如下:
QXmlStreamReader()
QXmlStreamReader(QIODevice * device)
QXmlStreamReader(const QByteArray & data)
QXmlStreamReader(const QString & data)
QXmlStreamReader(const char * data)
~QXmlStreamReader()
void setDevice(QIODevice * device)//以下四个都用于添加xml数据
void addData(const QByteArray & data)
void addData(const QString & data)
void addData(const char * data)
void addExtraNamespaceDeclaration(const QXmlStreamNamespaceDeclaration & extraNamespaceDeclaration)//添加额外命名空间申明
void addExtraNamespaceDeclarations(const QXmlStreamNamespaceDeclarations & extraNamespaceDeclarations)
void setEntityResolver(QXmlStreamEntityResolver * resolver)//添加实体解析器
void setNamespaceProcessing(bool)
bool atEnd() const//以下几个用于循环中,遍历元素(标签)
TokenType readNext()
QString readElementText(ReadElementTextBehaviour behaviour = ErrorOnUnexpectedElement)
bool readNextStartElement()
void skipCurrentElement()//跳过当前元素
void clear()
bool isDTD() const//以下判断当前遍历到的元素属于哪种类型
bool isCDATA() const
bool isCharacters() const
bool isComment() const
bool isStartDocument()const
bool isStartElement() const
bool isEndDocument() const
bool isEndElement() const
bool isEntityReference() const
bool isProcessingInstruction() const
bool isStandaloneDocument() const
bool isWhitespace() const
QStringRef documentEncoding() const
QStringRef documentVersion() const
QStringRef dtdName() const
QStringRef dtdPublicId() const
QStringRef dtdSystemId() const
qint64 lineNumber() const
QStringRef name() const//仅返回StartElement, EndElement或EntityReference的标签名
QStringRef prefix() const//返回的应是涉及命名空间的前辍
QStringRef text() const//仅返回Characters, Comment, DTD, 或 EntityReference.中的文本部分
QString tokenString() const
TokenType tokenType() const
QIODevice * device() const
qint64 characterOffset() const
qint64 columnNumber() const//返回当前解析到的行数
bool hasError() const//出错处理
Error error() const
QString errorString() const
void raiseError(const QString & message = QString())
QXmlStreamAttributes attributes() const
QXmlStreamEntityDeclarations entityDeclarations() const
QXmlStreamEntityResolver * entityResolver() const
QXmlStreamNamespaceDeclarations namespaceDeclarations() const
QStringRef processingInstructionData() const
QStringRef processingInstructionTarget() const
QStringRef qualifiedName() const
bool namespaceProcessing() const
QStringRef namespaceUri() const
QXmlStreamNotationDeclarations notationDeclarations() const
将上节的某篇抄过来,并添加一些部分,说明各部分与Qt对应的函数名:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--这是一个便签-->
<!DOCTYPE Notes [
<!ELEMENT note (date,message)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT message (#PCDATA)>
]>
<Notes>
<note>
<date>2月1日</date>
<message>别忘了参加party</message>
</note>
<note>
<?this is the most important ?>
<date>2月14日</date>
<message>今天情人节啊</message>
</note>
<note>
<date>2月18日</date>
<message>今天自己煮饭吧</message>
</note>
</Notes>
左边的为qt中的函数名,右端为对应XML部分
头部声明部分
StartDocument()——<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
documentVersion()——version="1.0"
documentEncoding()——encoding="UTF-8"
StandaloneDocument()——standalone="yes"
DTD部分
notationDeclarations()——<!ELEMENT note (date,message)>
entityDeclarations()——<!ELEMENT date (#PCDATA)><!ELEMENT message (#PCDATA)>
dtdName——Notes,<!DOCTYPE 后的那个,也即根元素名
dtdPublicId()和dtdSystemId()——注来自点此查看区别
实体部分:CDATA()——CDATA 部分中的所有内容都会被解析器忽略而直接有效,与注释不同,格式为:<![CDATA[" 内容 "]]>。见点击打开链接
EntityReference——实体引用
ProcessingInstruction()——处理指令,看下面的引用说明
引自:
http://zhidao.baidu.com/question/67891842.html?si=1
下面的一行就是在第二本书的定义之前的:
<?page render multiple authors ?>
虽然它看上去很像XML序言,但实际上是一种称为处理指令(processing instruction)的不同类型的语法。处理指令(以下简称PI)的目的是为了给处理页面的程序(例如XML解析器)提供额外的信息。PI通常情况下是没有固定格式的,唯一的要求是紧随第一个问号必须至少有一个字母。在此之后,PI可以包含除了小于号和大于号之外的任何字符串序列。最常见的PI是用来指定XML文件的样式表:这个PI一般会直接放在XML序言之后,通常由Web浏览器使用,来将XML数据以特殊的样式显示出来。
这个类自身有个TokenType(标签类型)用来描述当前解析到的文档部分,与上面我归纳的是近似的
enum QXmlStreamReader::TokenType
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 indocumentVersion(), 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 functionreadElementText() can be called to concatenate all content until the corresponding EndElement. Attributes are reported inattributes(), namespace declarations innamespaceDeclarations(). |
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 innotationDeclarations(), and entity declarations inentityDeclarations(). 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 inprocessingInstructionTarget() and processingInstructionData(). |
基础使用示例:
将上面XML代码保存为text.xml。
代码:
#include "dialog.h"
#include "ui_dialog.h"
#include <QFile>
#include <QIODevice>
#include <QDebug>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QFile *file=new QFile("://test.xml");
if(!file->open(QIODevice::ReadOnly))
{
qDebug()<<"文件打开失败";
return;
}
xmlReader =new QXmlStreamReader(file);
while(!xmlReader->atEnd() && !xmlReader->hasError())
{
xmlReader->readNext();
if(xmlReader->isStartElement())
{
if(xmlReader->name()=="note")
{
qDebug()<<"————note————";
}else if (xmlReader->name()=="date") {
qDebug()<<"date:"<<xmlReader->readElementText();
}else if (xmlReader->name()=="message") {
qDebug()<<"message:"<<xmlReader->readElementText();
}
}
}
}
Dialog::~Dialog()
{
delete ui;
}
输出为:
————note————
date: "2月1日"
message: "别忘了参加party"
————note————
date: "2月14日"
message: "今天情人节啊"
————note————
date: "2月18日"
message: "今天自己煮饭吧"
————————————————————————————————————————————————————————————————————
对于出错,错语类型枚举值有
enum QXmlStreamReader::Error
Constant | Value | Description |
---|---|---|
QXmlStreamReader::NoError | 0 | 无错误 |
QXmlStreamReader::CustomError | 2 | 自定义错误,通过raiseError()定义错误信息,不懂怎样使用,求赐教 |
QXmlStreamReader::NotWellFormedError | 3 | xml格式有问题,比如标签不配对,嵌套不正确。The parser internally raised an error due to the read XML not being well-formed. |
QXmlStreamReader::PrematureEndOfDocumentError | 4 | 来源的数据不完整,如通过网络传输来的The input stream ended before a well-formed XML document was parsed. Recovery from this error is possible if more XML arrives in the stream, either by calling addData() or by waiting for it to arrive on thedevice(). |
QXmlStreamReader::UnexpectedElementError | 1 | 意料之外的元素?求教The parser encountered an element that was different to those it expected. |