Qt:Документация 4.3.2/qtxml

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Видимо везде прийдется исправлять якоря абзацев)
Строка 1: Строка 1:
{{Qt4.3.2_header}}
{{Qt4.3.2_header}}
-
[Previous: [[Qt:Документация 4.3.2/qtsvg | QtSvg Module]]] [ [[Qt:Документация 4.3.2/modules | Qt's Modules]] ] [Next: [[Qt:Документация 4.3.2/qtdesigner | QtDesigner Module]]]
+
[Предыдущая: [[Qt:Документация 4.3.2/qtscript | Модуль QtScript]] ] [ [[Qt:Документация 4.3.2/modules | Модули Qt]] ] [Следующая: [[Qt:Документация 4.3.2/qtdesigner | Модуль QtDesigner]] ]  
=Модуль QtXml<br />=
=Модуль QtXml<br />=
-
Модуль QtXml обеспечивает работу с потоками чтения и записи XML документов и реализацию их в форме SAX и DOM. [[#Подробное описание | Далее...]]
+
 
 +
Модуль QtXml обеспечивает работу с потоками чтения и записи XML документов и реализацию их в форме SAX и DOM. [[#details | Далее...]]
==Классы==
==Классы==
 +
{|  width="100%" class="annotated" cellpadding="2" cellspacing="1" border="0"
{|  width="100%" class="annotated" cellpadding="2" cellspacing="1" border="0"
Строка 124: Строка 126:
|Запись XML с простым потоковым API
|Запись XML с простым потоковым API
|}
|}
-
 
+
<div id="details"></div>
==Подробное описание==
==Подробное описание==
 +
SAX is an event-based standard interface for XML parsers. The Qt interface follows the design of the SAX2 Java implementation. Its naming scheme was adapted to fit the Qt naming conventions. Details on SAX2 can be found at [http://www.saxproject.org http://www.saxproject.org].
SAX is an event-based standard interface for XML parsers. The Qt interface follows the design of the SAX2 Java implementation. Its naming scheme was adapted to fit the Qt naming conventions. Details on SAX2 can be found at [http://www.saxproject.org http://www.saxproject.org].
Строка 142: Строка 145:
This module is part of the [[Qt:Документация 4.3.2/commercialeditions#qt-console-edition | Qt Console Edition]], the [[Qt:Документация 4.3.2/commercialeditions#qt-desktop-edition | Qt Desktop Edition]], and the [[Qt:Документация 4.3.2/opensourceedition | Qt Open Source Edition]].
This module is part of the [[Qt:Документация 4.3.2/commercialeditions#qt-console-edition | Qt Console Edition]], the [[Qt:Документация 4.3.2/commercialeditions#qt-desktop-edition | Qt Desktop Edition]], and the [[Qt:Документация 4.3.2/opensourceedition | Qt Open Source Edition]].
 +
Topics:
 +
 +
*[[#configuring-the-build-process | Configuring the Build Process]]
 +
*[[#the-qtxml-stream-classes | The QtXml Stream Classes]]
 +
*[[#the-qt-sax2-classes | The Qt SAX2 Classes]]
 +
**[[#introduction-to-sax2 | Introduction to SAX2]]
 +
**[[#sax2-features | SAX2 Features]]
 +
**[[#namespace-support-via-features | Namespace Support via Features]]
 +
***[[#summary | Summary]]
 +
 +
**[[#properties | Свойства]]
 +
 +
*[[#the-qt-dom-classes | Классы Qt DOM]]
 +
**[[#introduction-to-dom | Введение в DOM]]
 +
 +
*[[#an-introduction-to-namespaces | An Introduction to Namespaces]]
 +
**[[#conventions-used-in-the-qt-xml-documentation | Conventions Used in the Qt XML Documentation]]
 +
 +
<div id="configuring-the-build-process"></div>
===Configuring the Build Process===
===Configuring the Build Process===
 +
Applications that use Qt's XML classes need to be configured to be built against the QtXml module. The following declaration in a <tt>qmake</tt> project file ensures that an application is compiled and linked appropriately:
Applications that use Qt's XML classes need to be configured to be built against the QtXml module. The following declaration in a <tt>qmake</tt> project file ensures that an application is compiled and linked appropriately:
<source lang="cpp-qt"> QT += xml</source>  
<source lang="cpp-qt"> QT += xml</source>  
This line is necessary because only the [[Qt:Документация 4.3.2/qtcore | QtCore]] and [[Qt:Документация 4.3.2/qtgui | QtGui]] modules are used in the default build process.
This line is necessary because only the [[Qt:Документация 4.3.2/qtcore | QtCore]] and [[Qt:Документация 4.3.2/qtgui | QtGui]] modules are used in the default build process.
-
 
+
<div id="the-qtxml-stream-classes"></div>
===The QtXml Stream Classes===
===The QtXml Stream Classes===
 +
The [[Qt:Документация 4.3.2/qxmlstreamreader | QXmlStreamReader]] and [[Qt:Документация 4.3.2/qxmlstreamwriter | QXmlStreamWriter]] are two new classes provided in Qt 4.3 and later. A stream reader reports an XML document as a stream of tokens. This differs from SAX as SAX applications provide handlers to receive XML events from the parser whereas the [[Qt:Документация 4.3.2/qxmlstreamreader | QXmlStreamReader]] drives the loop, pulling tokens from the reader when they are needed. This pulling approach makes it possible to build recursive descent parsers, allowing XML parsing code to be split into different methods or classes.
The [[Qt:Документация 4.3.2/qxmlstreamreader | QXmlStreamReader]] and [[Qt:Документация 4.3.2/qxmlstreamwriter | QXmlStreamWriter]] are two new classes provided in Qt 4.3 and later. A stream reader reports an XML document as a stream of tokens. This differs from SAX as SAX applications provide handlers to receive XML events from the parser whereas the [[Qt:Документация 4.3.2/qxmlstreamreader | QXmlStreamReader]] drives the loop, pulling tokens from the reader when they are needed. This pulling approach makes it possible to build recursive descent parsers, allowing XML parsing code to be split into different methods or classes.
Строка 204: Строка 228:
     writeEndDocument();
     writeEndDocument();
     return true;
     return true;
-
  }</source>  
+
  }</source> <div id="the-qt-sax2-classes"></div>
===The Qt SAX2 Classes===
===The Qt SAX2 Classes===
 +
<div id="introduction-to-sax2"></div>
====Introduction to SAX2====
====Introduction to SAX2====
 +
The SAX2 interface is an event-driven mechanism to provide the user with document information. An "event" in this context means something reported by the parser, for example, it has encountered a start tag, or an end tag, etc.
The SAX2 interface is an event-driven mechanism to provide the user with document information. An "event" in this context means something reported by the parser, for example, it has encountered a start tag, or an end tag, etc.
Строка 273: Строка 299:
The [[Qt:Документация 4.3.2/xml-saxbookmarks | SAX Bookmarks example]] illustrates how to subclass [[Qt:Документация 4.3.2/qxmldefaulthandler | QXmlDefaultHandler]] to read an XML bookmark file (XBEL) and how to generate XML by hand.
The [[Qt:Документация 4.3.2/xml-saxbookmarks | SAX Bookmarks example]] illustrates how to subclass [[Qt:Документация 4.3.2/qxmldefaulthandler | QXmlDefaultHandler]] to read an XML bookmark file (XBEL) and how to generate XML by hand.
-
 
+
<div id="sax2-features"></div>
====SAX2 Features====
====SAX2 Features====
 +
The behavior of an XML reader depends on its support for certain optional features. For example, a reader may have the feature "report attributes used for namespace declarations and prefixes along with the local name of a tag". Like every other feature this has a unique name represented by a URI: it is called ''http://xml.org/sax/features/namespace-prefixes''.
The behavior of an XML reader depends on its support for certain optional features. For example, a reader may have the feature "report attributes used for namespace declarations and prefixes along with the local name of a tag". Like every other feature this has a unique name represented by a URI: it is called ''http://xml.org/sax/features/namespace-prefixes''.
Строка 289: Строка 316:
[[Qt:Документация 4.3.2/qxmlsimplereader | QXmlSimpleReader]] is not validating, so it does not support ''http://xml.org/sax/features/validation''.
[[Qt:Документация 4.3.2/qxmlsimplereader | QXmlSimpleReader]] is not validating, so it does not support ''http://xml.org/sax/features/validation''.
-
 
+
<div id="namespace-support-via-features"></div>
====Namespace Support via Features====
====Namespace Support via Features====
 +
As we have seen in the previous section, we can configure the behavior of the reader when it comes to namespace processing. This is done by setting and unsetting the ''http://xml.org/sax/features/namespaces'' and ''http://xml.org/sax/features/namespace-prefixes'' features.
As we have seen in the previous section, we can configure the behavior of the reader when it comes to namespace processing. This is done by setting and unsetting the ''http://xml.org/sax/features/namespaces'' and ''http://xml.org/sax/features/namespace-prefixes'' features.
Строка 314: Строка 342:
As the SAX2 standard suggests, [[Qt:Документация 4.3.2/qxmlsimplereader | QXmlSimpleReader]] defaults to having ''http://xml.org/sax/features/namespaces'' set to true and ''http://xml.org/sax/features/namespace-prefixes'' set to false. When changing this behavior using [[Qt:Документация 4.3.2/qxmlsimplereader#setFeature | QXmlSimpleReader::setFeature]]() note that the combination of both features set to false is illegal.
As the SAX2 standard suggests, [[Qt:Документация 4.3.2/qxmlsimplereader | QXmlSimpleReader]] defaults to having ''http://xml.org/sax/features/namespaces'' set to true and ''http://xml.org/sax/features/namespace-prefixes'' set to false. When changing this behavior using [[Qt:Документация 4.3.2/qxmlsimplereader#setFeature | QXmlSimpleReader::setFeature]]() note that the combination of both features set to false is illegal.
-
 
+
<div id="summary"></div>
=====Summary=====
=====Summary=====
 +
[[Qt:Документация 4.3.2/qxmlsimplereader | QXmlSimpleReader]] implements the following behavior:
[[Qt:Документация 4.3.2/qxmlsimplereader | QXmlSimpleReader]] implements the following behavior:
Строка 350: Строка 379:
The behavior of the entries marked with an asterisk (*) is not specified by SAX.
The behavior of the entries marked with an asterisk (*) is not specified by SAX.
-
 
+
<div id="properties"></div>
====Свойства====
====Свойства====
 +
Свойства are a more general concept. They have a unique name, represented as an URI, but their value is <tt>void*</tt>. Thus nearly anything can be used as a property value. This concept involves some danger, though: there is no means of ensuring type-safety; the user must take care that they pass the right type. Свойства are useful if a reader supports special handler classes.
Свойства are a more general concept. They have a unique name, represented as an URI, but their value is <tt>void*</tt>. Thus nearly anything can be used as a property value. This concept involves some danger, though: there is no means of ensuring type-safety; the user must take care that they pass the right type. Свойства are useful if a reader supports special handler classes.
Строка 359: Строка 389:
To set or query properties the following functions are provided: [[Qt:Документация 4.3.2/qxmlreader#setProperty | QXmlReader::setProperty]](), [[Qt:Документация 4.3.2/qxmlreader#property | QXmlReader::property]]() and [[Qt:Документация 4.3.2/qxmlreader#hasProperty | QXmlReader::hasProperty]]().
To set or query properties the following functions are provided: [[Qt:Документация 4.3.2/qxmlreader#setProperty | QXmlReader::setProperty]](), [[Qt:Документация 4.3.2/qxmlreader#property | QXmlReader::property]]() and [[Qt:Документация 4.3.2/qxmlreader#hasProperty | QXmlReader::hasProperty]]().
-
 
+
<div id="dom"></div><div id="the-qt-dom-classes"></div>
===The Qt DOM Classes===
===The Qt DOM Classes===
 +
<div id="domintro"></div><div id="introduction-to-dom"></div>
====Introduction to DOM====
====Introduction to DOM====
 +
DOM provides an interface to access and change the content and structure of an XML file. It makes a hierarchical view of the document (a tree view). Thus -- in contrast to the SAX2 interface -- an object model of the document is resident in memory after parsing which makes manipulation easy.
DOM provides an interface to access and change the content and structure of an XML file. It makes a hierarchical view of the document (a tree view). Thus -- in contrast to the SAX2 interface -- an object model of the document is resident in memory after parsing which makes manipulation easy.
Строка 422: Строка 454:
To get started please refer to the [[Qt:Документация 4.3.2/qdomdocument | QDomDocument]] documentation. You might also want to take a look at the [[Qt:Документация 4.3.2/xml-dombookmarks | DOM Bookmarks example]], which illustrates how to read and write an XML bookmark file (XBEL) using DOM.
To get started please refer to the [[Qt:Документация 4.3.2/qdomdocument | QDomDocument]] documentation. You might also want to take a look at the [[Qt:Документация 4.3.2/xml-dombookmarks | DOM Bookmarks example]], which illustrates how to read and write an XML bookmark file (XBEL) using DOM.
-
 
+
<div id="namespaces"></div><div id="an-introduction-to-namespaces"></div>
===An Introduction to Namespaces===
===An Introduction to Namespaces===
 +
Parts of the Qt XML module documentation assume that you are familiar with XML namespaces. Here we present a brief introduction; skip to [[#namespacesconventions | Qt XML documentation conventions]] if you already know this material.
Parts of the Qt XML module documentation assume that you are familiar with XML namespaces. Here we present a brief introduction; skip to [[#namespacesconventions | Qt XML documentation conventions]] if you already know this material.
Строка 489: Строка 522:
More information on XML namespaces can be found at [http://www.w3.org/TR/REC-xml-names/ http://www.w3.org/TR/REC-xml-names/].
More information on XML namespaces can be found at [http://www.w3.org/TR/REC-xml-names/ http://www.w3.org/TR/REC-xml-names/].
-
 
+
<div id="namespacesconventions"></div><div id="conventions-used-in-the-qt-xml-documentation"></div>
====Conventions Used in the Qt XML Documentation====
====Conventions Used in the Qt XML Documentation====
 +
The following terms are used to distinguish the parts of names within the context of namespaces:
The following terms are used to distinguish the parts of names within the context of namespaces:
Строка 502: Строка 536:
See also [[Qt:Документация 4.3.2/xml-dombookmarks | DOM Bookmarks Example]] and [[Qt:Документация 4.3.2/xml-saxbookmarks | SAX Bookmarks Example]].
See also [[Qt:Документация 4.3.2/xml-dombookmarks | DOM Bookmarks Example]] and [[Qt:Документация 4.3.2/xml-saxbookmarks | SAX Bookmarks Example]].
-
[Previous: [[Qt:Документация 4.3.2/qtsvg | QtSvg Module]]] [ [[Qt:Документация 4.3.2/modules | Qt's Modules]] ] [Next: [[Qt:Документация 4.3.2/qtdesigner | QtDesigner Module]]]
+
[Previous: [[Qt:Документация 4.3.2/qtsvg | QtSvg Module]] ] [ [[Qt:Документация 4.3.2/modules | Qt's Modules]] ] [Next: [[Qt:Документация 4.3.2/qtdesigner | QtDesigner Module]] ]  
{{Qt4.3.2_footer}}
{{Qt4.3.2_footer}}

Версия 10:48, 6 ноября 2008

40px Внимание: Актуальная версия перевода документации находится здесь

__NOTOC__

Image:qt-logo.png

Главная · Все классы · Основные классы · Классы по группам · Модули · Функции

Image:trolltech-logo.png

[Предыдущая: Модуль QtScript ] [ Модули Qt ] [Следующая: Модуль QtDesigner ]

Содержание

Модуль QtXml

Модуль QtXml обеспечивает работу с потоками чтения и записи XML документов и реализацию их в форме SAX и DOM. Далее...

Классы

QDomAttr Представляет один атрибут QDomElement
QDomCDATASection Представляет в XML область CDATA
QDomCharacterData Представляет общие строки в DOM
QDomComment Представляет XML комментарий
QDomDocument Представляет XML документ
QDomDocumentFragment Дерево из QDomNodes, которое, как правило, не является целым QDomDocument
QDomDocumentType Представление о DTD в дереве документа
QDomElement Представляет один элемент в DOM дереве
QDomEntity Представляет XML сущность
QDomEntityReference Представляет ссылку на XML сущность
QDomImplementation Информация о возможностях представления DOM
QDomNamedNodeMap Коллекция узлов, которые могут быть доступны по имени
QDomNode Базовый класс для всех узлов в DOM дереве
QDomNodeList Список объектов QDomNode
QDomNotation Представление XML нотации
QDomProcessingInstruction Представление инструкций обработки XML
QDomText Представляет текстовые данные в разбираемом XML документе
QXmlAttributes XML атрибуты
QXmlContentHandler Интрефейс для описания логического содержания XML данных
QXmlDTDHandler Интерфейс для описания DTD, содержащимся в XML данных
QXmlDeclHandler Интерфейс для описания декларации содержимого XML данных
QXmlDefaultHandler Представление по-умолчанию всех классов XML-обработчиков
QXmlEntityResolver Интерфейс для разбора внешних сущностей, содержащихся в XML-данных
QXmlErrorHandler Интерфейс для сообщений об ошибках в XML-данных
QXmlInputSource Входящие данные для подклассов QXmlReader
QXmlLexicalHandler Интерфейс для сообщений о лексическом содержании XML-данных
QXmlLocator Обработчик XML классов с информацией о позиции разбора в файле
QXmlNamespaceSupport Вспомогательный класс для чтения XML с поддержкой пространств имен
QXmlParseException Используется для сообщений об ошибках с интерфейсом QXmlErrorHandler
QXmlReader Интерфейс для чтения XML (т.е. анализаторов)
QXmlSimpleReader Реализация простого XML анализатора
QXmlStreamAttribute Представление одиночного XML атрибута
QXmlStreamAttributes Представление вектора QXmlStreamAttribute
QXmlStreamEntityDeclaration Представление декларации DTD сущностей
QXmlStreamNamespaceDeclaration Представление декларации пространств имен
QXmlStreamNotationDeclaration Представление декларации DTD нотации
QXmlStreamReader Быстро и корректно сформированный XML-анализатор с простым потоковым API
QXmlStreamWriter Запись XML с простым потоковым API

Подробное описание

SAX is an event-based standard interface for XML parsers. The Qt interface follows the design of the SAX2 Java implementation. Its naming scheme was adapted to fit the Qt naming conventions. Details on SAX2 can be found at http://www.saxproject.org.

Support for SAX2 filters and the reader factory are under development. The Qt implementation does not include the SAX1 compatibility classes present in the Java interface. For an introduction to Qt's SAX2 classes, see The Qt SAX2 Classes.

DOM Level 2 is a W3C Recommendation for XML interfaces that maps the constituents of an XML document to a tree structure. The specification of DOM Level 2 can be found at http://www.w3.org/DOM/. For more information about the DOM classes in Qt is provided, see The Qt DOM Classes.

Since version 4.3, Qt provides two new classes for reading and writing XML: QXmlStreamReader and QXmlStreamWriter.

In addition to core XML support, QSvgRenderer can read a subset of SVG. Additional XML support is provided by the Qt Solutions group who provide, for example, classes that support SOAP and MML with the Qt XML classes.

To include the definitions of the module's classes, use the following directive:

 #include <QtXml>

To link against the module, add this line to your qmake .pro file:

 QT += xml

This module is part of the Qt Console Edition, the Qt Desktop Edition, and the Qt Open Source Edition.

Topics:

Configuring the Build Process

Applications that use Qt's XML classes need to be configured to be built against the QtXml module. The following declaration in a qmake project file ensures that an application is compiled and linked appropriately:

 QT += xml

This line is necessary because only the QtCore and QtGui modules are used in the default build process.

The QtXml Stream Classes

The QXmlStreamReader and QXmlStreamWriter are two new classes provided in Qt 4.3 and later. A stream reader reports an XML document as a stream of tokens. This differs from SAX as SAX applications provide handlers to receive XML events from the parser whereas the QXmlStreamReader drives the loop, pulling tokens from the reader when they are needed. This pulling approach makes it possible to build recursive descent parsers, allowing XML parsing code to be split into different methods or classes.

QXmlStreamReader is a well-formed XML 1.0 parser that excludes external parsed entities. Hence, data provided by the stream reader adheres to the W3C's criteria for well-formed XML, as long as no error occurs. Otherwise, functions such as atEnd(), error() and hasError() can be used to check and view the errors.

An example of QXmlStreamReader implementation would be the XbelReader in QXmlStream Bookmarks Example, which is a subclass of QXmlStreamReder. The constructor takes treeWidget as a parameter and the class has Xbel specific functions:

     XbelReader(QTreeWidget *treeWidget);
     ...
     void readUnknownElement();
     void readXBEL();
     void readTitle(QTreeWidgetItem *item);
     void readSeparator(QTreeWidgetItem *item);
     void readFolder(QTreeWidgetItem *item);
     void readBookmark(QTreeWidgetItem *item);
 
     QTreeWidgetItem *createChildItem(QTreeWidgetItem *item);
 
     QTreeWidget *treeWidget;
     ...

The read() function accepts a QIODevice and sets it with setDevice(). The raiseError() function is used to display a custom error message, inidicating that the file's version is incorrect.

 bool XbelReader::read(QIODevice *device)
 {
     setDevice(device);
 
     while (!atEnd()) {
         readNext();
 
         if (isStartElement()) {
             if (name() == "xbel" &amp;&amp; attributes().value("version") == "1.0")
                 readXBEL();
             else
                 raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
         }
     }
 
     return !error();
 }

The pendent to QXmlStreamReader is QXmlStreamWriter, which provides an XML writer with a simple streaming API. QXmlStreamWriter operates on a QIODevice and has specialised functions for all XML tokens or events you want to write, such as writeDTD(), writeCharacters(), writeComment() and so on.

To write XML document with QXmlStreamWriter, you start a document with the writeStartDocument() function and end it with writeEndDocument(), which implicitly closes all remaining open tags. Element tags are opened with writeStartDocument() and followed by writeAttribute() or writeAttributes(), element content, and then writeEndDocument(). Also, writeEmptyElement() can be used to write empty elements.

Element content comprises characters, entity references or nested elements. Content can be written with writeCharacters(), a function that also takes care of escaping all forbidden characters and character sequences, writeEntityReference(), or subsequent calls to writeStartElement().

The XbelWriter class from QXmlStream Bookmarks Example is a subclass of QXmlStreamWriter. Its writeFile() function illustrates the core functions of QXmlStreamWriter mentioned above:

 bool XbelWriter::writeFile(QIODevice *device)
 {
     setDevice(device);
 
     writeStartDocument();
     writeDTD("<!DOCTYPE xbel>");
     writeStartElement("xbel");
     writeAttribute("version", "1.0");
     for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
         writeItem(treeWidget->topLevelItem(i));
 
     writeEndDocument();
     return true;
 }

The Qt SAX2 Classes

Introduction to SAX2

The SAX2 interface is an event-driven mechanism to provide the user with document information. An "event" in this context means something reported by the parser, for example, it has encountered a start tag, or an end tag, etc.

To make it less abstract consider the following example:

 <quote>A quotation.</quote>

Whilst reading (a SAX2 parser is usually referred to as "reader") the above document three events would be triggered:

  1. A start tag occurs (<quote>).
  2. Character data (i.e. text) is found, "A quotation.".
  3. An end tag is parsed (</quote>).

Each time such an event occurs the parser reports it; you can set up event handlers to respond to these events.

Whilst this is a fast and simple approach to read XML documents, manipulation is difficult because data is not stored, simply handled and discarded serially. The DOM interface reads in and stores the whole document in a tree structure; this takes more memory, but makes it easier to manipulate the document's structure..

The Qt XML module provides an abstract class, QXmlReader, that defines the interface for potential SAX2 readers. Qt includes a reader implementation, QXmlSimpleReader, that is easy to adapt through subclassing.

The reader reports parsing events through special handler classes:


Handler class Description
QXmlContentHandler Reports events related to the content of a document (e.g. the start tag or characters).
QXmlDTDHandler Reports events related to the DTD (e.g. notation declarations).
QXmlErrorHandler Reports errors or warnings that occurred during parsing.
QXmlEntityResolver Reports external entities during parsing and allows users to resolve external entities themselves instead of leaving it to the reader.
QXmlDeclHandler Reports further DTD related events (e.g. attribute declarations).
QXmlLexicalHandler Reports events related to the lexical structure of the document (the beginning of the DTD, comments etc.).

These classes are abstract classes describing the interface. The QXmlDefaultHandler class provides a "do nothing" default implementation for all of them. Therefore users only need to overload the QXmlDefaultHandler functions they are interested in.

To read input XML data a special class QXmlInputSource is used.

Apart from those already mentioned, the following SAX2 support classes provide additional useful functionality:


Class Description
QXmlAttributes Used to pass attributes in a start element event.
QXmlLocator Used to obtain the actual parsing position of an event.
QXmlNamespaceSupport Used to implement namespace support for a reader. Note that namespaces do not change the parsing behavior. They are only reported through the handler.

The SAX Bookmarks example illustrates how to subclass QXmlDefaultHandler to read an XML bookmark file (XBEL) and how to generate XML by hand.

SAX2 Features

The behavior of an XML reader depends on its support for certain optional features. For example, a reader may have the feature "report attributes used for namespace declarations and prefixes along with the local name of a tag". Like every other feature this has a unique name represented by a URI: it is called http://xml.org/sax/features/namespace-prefixes.

The Qt SAX2 implementation can report whether the reader has particular functionality using the QXmlReader::hasFeature() function. Available features can be tested with QXmlReader::feature(), and switched on or off using QXmlReader::setFeature().

Consider the example

 <document xmlns:book = 'http://trolltech.com/fnord/book/'
           xmlns      = 'http://trolltech.com/fnord/' >

A reader that does not support the http://xml.org/sax/features/namespace-prefixes feature would report the element name document but not its attributes xmlns:book and xmlns with their values. A reader with the feature http://xml.org/sax/features/namespace-prefixes reports the namespace attributes if the feature is switched on.

Other features include http://xml.org/sax/features/namespace (namespace processing, implies http://xml.org/sax/features/namespace-prefixes) and http://xml.org/sax/features/validation (the ability to report validation errors).

Whilst SAX2 leaves it to the user to define and implement whatever features are required, support for http://xml.org/sax/features/namespace (and thus http://xml.org/sax/features/namespace-prefixes) is mandantory. The QXmlSimpleReader implementation of QXmlReader, supports them, and can do namespace processing.

QXmlSimpleReader is not validating, so it does not support http://xml.org/sax/features/validation.

Namespace Support via Features

As we have seen in the previous section, we can configure the behavior of the reader when it comes to namespace processing. This is done by setting and unsetting the http://xml.org/sax/features/namespaces and http://xml.org/sax/features/namespace-prefixes features.

They influence the reporting behavior in the following way:

  1. Namespace prefixes and local parts of elements and attributes can be reported.
  2. The qualified names of elements and attributes are reported.
  3. QXmlContentHandler::startPrefixMapping() and QXmlContentHandler::endPrefixMapping() are called by the reader.
  4. Attributes that declare namespaces (i.e. the attribute xmlns and attributes starting with xmlns:) are reported.

Consider the following element:

 <author xmlns:fnord = 'http://trolltech.com/fnord/'
              title="Ms"
              fnord:title="Goddess"
              name="Eris Kallisti"/>

With http://xml.org/sax/features/namespace-prefixes set to true the reader will report four attributes; but with the namespace-prefixes feature set to false only three, with the xmlns:fnord attribute defining a namespace being "invisible" to the reader.

The http://xml.org/sax/features/namespaces feature is responsible for reporting local names, namespace prefixes and URIs. With http://xml.org/sax/features/namespaces set to true the parser will report title as the local name of the fnord:title attribute, fnord being the namespace prefix and http://trolltech.com/fnord/ as the namespace URI. When http://xml.org/sax/features/namespaces is false none of them are reported.

In the current implementation the Qt XML classes follow the definition that the prefix xmlns itself isn't associated with any namespace at all (see http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-using). Therefore even with http://xml.org/sax/features/namespaces and http://xml.org/sax/features/namespace-prefixes both set to true the reader won't return either a local name, a namespace prefix or a namespace URI for xmlns:fnord.

This might be changed in the future following the W3C suggestion http://www.w3.org/2000/xmlns/ to associate xmlns with the namespace http://www.w3.org/2000/xmlns.

As the SAX2 standard suggests, QXmlSimpleReader defaults to having http://xml.org/sax/features/namespaces set to true and http://xml.org/sax/features/namespace-prefixes set to false. When changing this behavior using QXmlSimpleReader::setFeature() note that the combination of both features set to false is illegal.

Summary

QXmlSimpleReader implements the following behavior:


(namespaces, namespace-prefixes) Namespace prefix and local part Qualified names Prefix mapping xmlns attributes
(true, false) Yes Yes* Yes No
(true, true) Yes Yes Yes Yes
(false, true) No* Yes No* Yes
(false, false) Illegal

The behavior of the entries marked with an asterisk (*) is not specified by SAX.

Свойства

Свойства are a more general concept. They have a unique name, represented as an URI, but their value is void*. Thus nearly anything can be used as a property value. This concept involves some danger, though: there is no means of ensuring type-safety; the user must take care that they pass the right type. Свойства are useful if a reader supports special handler classes.

The URIs used for features and properties often look like URLs, e.g. http://xml.org/sax/features/namespace. This does not mean that the data required is at this address. It is simply a way of defining unique names.

Anyone can define and use new SAX2 properties for their readers. Property support is not mandatory.

To set or query properties the following functions are provided: QXmlReader::setProperty(), QXmlReader::property() and QXmlReader::hasProperty().

The Qt DOM Classes

Introduction to DOM

DOM provides an interface to access and change the content and structure of an XML file. It makes a hierarchical view of the document (a tree view). Thus -- in contrast to the SAX2 interface -- an object model of the document is resident in memory after parsing which makes manipulation easy.

All DOM nodes in the document tree are subclasses of QDomNode. The document itself is represented as a QDomDocument object.

Here are the available node classes and their potential child classes:

With QDomNodeList and QDomNamedNodeMap two collection classes are provided: QDomNodeList is a list of nodes, and QDomNamedNodeMap is used to handle unordered sets of nodes (often used for attributes).

The QDomImplementation class allows the user to query features of the DOM implementation.

To get started please refer to the QDomDocument documentation. You might also want to take a look at the DOM Bookmarks example, which illustrates how to read and write an XML bookmark file (XBEL) using DOM.

An Introduction to Namespaces

Parts of the Qt XML module documentation assume that you are familiar with XML namespaces. Here we present a brief introduction; skip to Qt XML documentation conventions if you already know this material.

Namespaces are a concept introduced into XML to allow a more modular design. With their help data processing software can easily resolve naming conflicts in XML documents.

Consider the following example:

 <document>
 <book>
   <title>Practical XML</title>
   <author title="Ms" name="Eris Kallisti"/>
   <chapter>
     <title>A Namespace Called fnord</title>
   </chapter>
 </book>
 </document>

Here we find three different uses of the name title. If you wish to process this document you will encounter problems because each of the titles should be displayed in a different manner -- even though they have the same name.

The solution would be to have some means of identifying the first occurrence of title as the title of a book, i.e. to use the title element of a book namespace to distinguish it from, for example, the chapter title, e.g.:

 <book:title>Practical XML</book:title>

book in this case is a prefix denoting the namespace.

Before we can apply a namespace to element or attribute names we must declare it.

Namespaces are URIs like http://trolltech.com/fnord/book/. This does not mean that data must be available at this address; the URI is simply used to provide a unique name.

We declare namespaces in the same way as attributes; strictly speaking they are attributes. To make for example http://trolltech.com/fnord/ the document's default XML namespace xmlns we write

 xmlns="http://trolltech.com/fnord/"

To distinguish the http://trolltech.com/fnord/book/ namespace from the default, we must supply it with a prefix:

 xmlns:book="http://trolltech.com/fnord/book/"

A namespace that is declared like this can be applied to element and attribute names by prepending the appropriate prefix and a ":" delimiter. We have already seen this with the book:title element.

Element names without a prefix belong to the default namespace. This rule does not apply to attributes: an attribute without a prefix does not belong to any of the declared XML namespaces at all. Attributes always belong to the "traditional" namespace of the element in which they appear. A "traditional" namespace is not an XML namespace, it simply means that all attribute names belonging to one element must be different. Later we will see how to assign an XML namespace to an attribute.

Due to the fact that attributes without prefixes are not in any XML namespace there is no collision between the attribute title (that belongs to the author element) and for example the title element within a chapter.

Let's clarify this with an example:

 <document xmlns:book = 'http://trolltech.com/fnord/book/'
           xmlns      = 'http://trolltech.com/fnord/' >
 <book>
   <book:title>Practical XML</book:title>
   <book:author xmlns:fnord = 'http://trolltech.com/fnord/'
                title="Ms"
                fnord:title="Goddess"
                name="Eris Kallisti"/>
   <chapter>
     <title>A Namespace Called fnord</title>
   </chapter>
 </book>
 </document>

Within the document element we have two namespaces declared. The default namespace http://trolltech.com/fnord/ applies to the book element, the chapter element, the appropriate title element and of course to document itself.

The book:author and book:title elements belong to the namespace with the URI http://trolltech.com/fnord/book/.

The two book:author attributes title and name have no XML namespace assigned. They are only members of the "traditional" namespace of the element book:author, meaning that for example two title attributes in book:author are forbidden.

In the above example we circumvent the last rule by adding a title attribute from the http://trolltech.com/fnord/ namespace to book:author: the fnord:title comes from the namespace with the prefix fnord that is declared in the book:author element.

Clearly the fnord namespace has the same namespace URI as the default namespace. So why didn't we simply use the default namespace we'd already declared? The answer is quite complex:

  • attributes without a prefix don't belong to any XML namespace at all, not even to the default namespace;
  • additionally omitting the prefix would lead to a title-title clash;
  • writing it as xmlns:title would declare a new namespace with the prefix title instead of applying the default xmlns namespace.

With the Qt XML classes elements and attributes can be accessed in two ways: either by refering to their qualified names consisting of the namespace prefix and the "real" name (or local name) or by the combination of local name and namespace URI.

More information on XML namespaces can be found at http://www.w3.org/TR/REC-xml-names/.

Conventions Used in the Qt XML Documentation

The following terms are used to distinguish the parts of names within the context of namespaces:

  • The qualified name is the name as it appears in the document. (In the above example book:title is a qualified name.)
  • A namespace prefix in a qualified name is the part to the left of the ":". (book is the namespace prefix in book:title.)
  • The local part of a name (also refered to as the local name) appears to the right of the ":". (Thus title is the local part of book:title.)
  • The namespace URI ("Uniform Resource Identifier") is a unique identifier for a namespace. It looks like a URL (e.g. http://trolltech.com/fnord/ ) but does not require data to be accessible by the given protocol at the named address.

Elements without a ":" (like chapter in the example) do not have a namespace prefix. In this case the local part and the qualified name are identical (i.e. chapter).

See also DOM Bookmarks Example and SAX Bookmarks Example.

[Previous: QtSvg Module ] [ Qt's Modules ] [Next: QtDesigner Module ]


Copyright © 2007 Trolltech Trademarks
Qt 4.3.2