Qt模块化笔记之core——QXmlStreamReader的几个函数返回值类

来源:互联网 发布:js file读取文件路径 编辑:程序博客网 时间:2024/04/29 21:00

本文主要讨论QXmlStreamReader的几个函数返回值所涉及的类、

QXmlStreamReader的函数为:

QXmlStreamAttributesattributes() constQXmlStreamEntityDeclarationsentityDeclarations() constQXmlStreamEntityResolver *entityResolver() constQXmlStreamNamespaceDeclarationsnamespaceDeclarations() constQXmlStreamNotationDeclarationsnotationDeclarations() const

————————————————————————————————————————————————————————————

1, QXmlStreamNamespaceDeclarationQXmlStreamNamespaceDeclarations

两个类都有关“XML流的命名空间声明”,关系为(The QXmlStreamNamespaceDeclarations class is defined to be a QVector of QXmlStreamNamespaceDeclaration.)

QXmlStreamNamespaceDeclarations QXmlStreamReader::namespaceDeclarations() const
上面这个函数,如果 tokenType()为StartElement,则它返回“XML流的命名空间声明”,否则,空的vector返回。
为本函数准备的XML代码为:

<?xml version="1.0" encoding="UTF-8"?>  <tables>   <table xmlns:h="http://www.w3.org/TR/html4/">     <tr>      <td>Apples</td>      <td>Bananas</td>     </tr>   </table>   <table xmlns:f="http://www.w3school.com.cn/furniture">     <name>African Coffee Table</name>     <width>80</width>     <length>120</length>   </table>  </tables>  
测试C++代码为:

#include "dialog.h"#include "ui_dialog.h"#include <QFile>#include <QIODevice>#include <QDebug>#include <QXmlStreamNamespaceDeclarations>Dialog::Dialog(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog){    ui->setupUi(this);    QFile *file=new QFile("://test2.xml");    if(!file->open(QIODevice::ReadOnly))    {        qDebug()<<"文件打开失败";        return;    }    xmlReader =new QXmlStreamReader(file);    while(!xmlReader->atEnd() && !xmlReader->hasError())    {        xmlReader->readNext();        if(xmlReader->tokenType()==QXmlStreamReader::StartElement)        {            QXmlStreamNamespaceDeclarations ns=xmlReader->namespaceDeclarations();            foreach (QXmlStreamNamespaceDeclaration n, ns) {                qDebug()<<n.prefix()<<" "<<n.namespaceUri();                qDebug()<<"——————";            }        }    }}Dialog::~Dialog(){    delete ui;}
输出为:

"h" "http://www.w3.org/TR/html4/"

——————

"f" "http://www.w3school.com.cn/furniture"

—————— 


————————————————————————————————————————————————————————————

2,QXmlStreamEntityDeclarations与QXmlStreamEntityDeclaration关系与上述相同

QXmlStreamEntityDeclarationsentityDeclarations() const
如果 tokenType()是DTD,返回的是未解析的原实体声明,即整个:

<!DOCTYPE Notes [      <!ELEMENT note (date,message)>      <!ELEMENT date (#PCDATA)>      <!ELEMENT message (#PCDATA)>  ]>
否则是个空的vector

QXmlStreamEntityDeclaration公有函数有:

QXmlStreamEntityDeclaration()QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration & other)~QXmlStreamEntityDeclaration()QStringRefname() constQStringRefnotationName() constQStringRefvalue() constQStringRefpublicId() constQStringRefsystemId() constbooloperator!=(const QXmlStreamEntityDeclaration & other) constQXmlStreamEntityDeclaration &operator=(const QXmlStreamEntityDeclaration & other)booloperator==(const QXmlStreamEntityDeclaration & other) const
测试的XML代码如下:

<?xml version="1.0"?><!DOCTYPE note [  <!ELEMENT note (to,from,heading,body)>  <!ELEMENT to      (#PCDATA)>  <!ELEMENT from    (#PCDATA)>  <!ELEMENT heading (#PCDATA)>  <!ELEMENT body    (#PCDATA)>]><note>  <to>George</to>  <from>John</from>  <heading>Reminder</heading>  <body>Don't forget the meeting!</body></note>

qt代码如下:
#include "dialog.h"#include "ui_dialog.h"#include <QFile>#include <QIODevice>#include <QDebug>#include <QXmlStreamNamespaceDeclarations>#include <QXmlStreamEntityDeclarations>#include <QXmlStreamEntityDeclaration>Dialog::Dialog(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog){    ui->setupUi(this);    //——————————————————————    QFile *file2=new QFile("://test3.xml");    if(!file2->open(QIODevice::ReadOnly))    {        qDebug()<<"文件打开失败";        return;    }    xmlReader2 =new QXmlStreamReader(file2);    while(!xmlReader2->atEnd() && !xmlReader2->hasError())    {        if(xmlReader2->readNext()==QXmlStreamReader::DTD)        {            QXmlStreamEntityDeclarations ns=xmlReader2->entityDeclarations();//            foreach (QXmlStreamEntityDeclaration n, ns) {//                qDebug()<<"行:"<<n.name()<<n.notationName()<<n.publicId()<<n.systemId()<<n.value();//            }            QVectorIterator<QXmlStreamEntityDeclaration> it(ns);            while (it.hasNext()) {                QXmlStreamEntityDeclaration n=it.next();                qDebug()<<"行:"<<n.name()<<n.notationName()<<n.publicId()<<n.systemId()<<n.value();            }        }    }}Dialog::~Dialog(){    delete ui;}

让我想不通,这里没输出……
————————————————————————————————————————————————————————————

3,QXmlStreamEntityResolver

未知功能,应该与这里有关:点击打开链接

————————————————————————————————————————————————————————————

4,QXmlStreamNotationDeclaration DTD标记声明
其公有函数如下:
QXmlStreamNotationDeclaration()QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration & other)~QXmlStreamNotationDeclaration()QStringRefname() constQStringRefpublicId() constQStringRefsystemId() constbooloperator!=(const QXmlStreamNotationDeclaration & other) constQXmlStreamNotationDeclaration &operator=(const QXmlStreamNotationDeclaration & other)booloperator==(const QXmlStreamNotationDeclaration & other) const

————————————————————————————————————————————————————————————
本文较多漏洞,虚心求教……

0 0
原创粉丝点击