| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | /*! | 
|---|
| 43 | \module QtXml | 
|---|
| 44 | \title QtXml Module | 
|---|
| 45 | \contentspage Qt's Modules | 
|---|
| 46 | \previouspage QtSvg | 
|---|
| 47 | \nextpage QtXmlPatterns | 
|---|
| 48 | \ingroup modules | 
|---|
| 49 |  | 
|---|
| 50 | \brief The QtXml module provides a stream reader and writer for | 
|---|
| 51 | XML documents, and C++ implementations of SAX and DOM. | 
|---|
| 52 |  | 
|---|
| 53 | SAX is an event-based standard interface for XML parsers. | 
|---|
| 54 | The Qt interface follows the design of the SAX2 Java implementation. | 
|---|
| 55 | Its naming scheme was adapted to fit the Qt naming conventions. | 
|---|
| 56 | Details on SAX2 can be found at \l{http://www.saxproject.org}. | 
|---|
| 57 |  | 
|---|
| 58 | Support for SAX2 filters and the reader factory are under | 
|---|
| 59 | development. The Qt implementation does not include the SAX1 | 
|---|
| 60 | compatibility classes present in the Java interface. | 
|---|
| 61 | For an introduction to Qt's SAX2 classes, see \l{The Qt SAX2 Classes}. | 
|---|
| 62 |  | 
|---|
| 63 | DOM Level 2 is a W3C Recommendation for XML interfaces that maps the | 
|---|
| 64 | constituents of an XML document to a tree structure. The specification | 
|---|
| 65 | of DOM Level 2 can be found at \l{http://www.w3.org/DOM/}. | 
|---|
| 66 | For more information about the DOM classes in Qt is provided, see | 
|---|
| 67 | \l{The Qt DOM Classes}. | 
|---|
| 68 |  | 
|---|
| 69 | Since version 4.3, Qt provides two new classes for reading and | 
|---|
| 70 | writing XML: QXmlStreamReader and QXmlStreamWriter. | 
|---|
| 71 |  | 
|---|
| 72 | In addition to core XML support, classes for higher level querying | 
|---|
| 73 | and manipulation of XML data, are provided by the QtXmlPatterns | 
|---|
| 74 | module. In the QtSvg module, the QSvgRenderer and QSvgGenerator | 
|---|
| 75 | classes can read and write a subset of SVG, an XML-based file | 
|---|
| 76 | format. Qt also provides helper functions that may be useful to | 
|---|
| 77 | those working with XML and XHTML: see Qt::escape() and | 
|---|
| 78 | Qt::convertFromPlainText(). | 
|---|
| 79 |  | 
|---|
| 80 | Further XML support is provided by the \l{Qt Solutions} group who | 
|---|
| 81 | provide, for example, classes that support SOAP and MML with the | 
|---|
| 82 | Qt XML classes. | 
|---|
| 83 |  | 
|---|
| 84 | This module is part of the \l{Qt Full Framework Edition} and the | 
|---|
| 85 | \l{Open Source Versions of Qt}. | 
|---|
| 86 |  | 
|---|
| 87 | Topics: | 
|---|
| 88 |  | 
|---|
| 89 | \tableofcontents | 
|---|
| 90 |  | 
|---|
| 91 | \section1 Configuring the Build Process | 
|---|
| 92 |  | 
|---|
| 93 | Applications that use Qt's XML classes need to be configured to | 
|---|
| 94 | be built against the QtXml module. The following declaration in a | 
|---|
| 95 | \c qmake project file ensures that an application is compiled and | 
|---|
| 96 | linked appropriately: | 
|---|
| 97 |  | 
|---|
| 98 | To include the definitions of the module's classes, use the | 
|---|
| 99 | following directive: | 
|---|
| 100 |  | 
|---|
| 101 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 0 | 
|---|
| 102 |  | 
|---|
| 103 | To link against the module, add this line to your \l qmake \c | 
|---|
| 104 | .pro file: | 
|---|
| 105 |  | 
|---|
| 106 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 1 | 
|---|
| 107 |  | 
|---|
| 108 | This line is necessary because only the QtCore and QtGui modules | 
|---|
| 109 | are used in the default build process. | 
|---|
| 110 |  | 
|---|
| 111 | \section1 The QtXml Stream Classes | 
|---|
| 112 |  | 
|---|
| 113 | The QXmlStreamReader and QXmlStreamWriter are two new classes provided | 
|---|
| 114 | in Qt 4.3 and later. A stream reader reports an XML document as a stream | 
|---|
| 115 | of tokens. This differs from SAX as SAX applications provide handlers to | 
|---|
| 116 | receive XML events from the parser whereas the QXmlStreamReader drives the | 
|---|
| 117 | loop, pulling tokens from the reader when they are needed. | 
|---|
| 118 | This pulling approach makes it possible to build recursive descent parsers, | 
|---|
| 119 | allowing XML parsing code to be split into different methods or classes. | 
|---|
| 120 |  | 
|---|
| 121 | QXmlStreamReader is a well-formed XML 1.0 parser that excludes external | 
|---|
| 122 | parsed entities. Hence, data provided by the stream reader adheres to the | 
|---|
| 123 | W3C's criteria for well-formed XML, as long as no error occurs. Otherwise, | 
|---|
| 124 | functions such as \l{QXmlStreamReader::atEnd()}{atEnd()}, | 
|---|
| 125 | \l{QXmlStreamReader::error()}{error()} and \l{QXmlStreamReader::hasError()} | 
|---|
| 126 | {hasError()} can be used to check and view the errors. | 
|---|
| 127 |  | 
|---|
| 128 | An example of QXmlStreamReader implementation would be the \c XbelReader in | 
|---|
| 129 | \l{QXmlStream Bookmarks Example}, which is a subclass of QXmlStreamReader. | 
|---|
| 130 | The constructor takes \a treeWidget as a parameter and the class has Xbel | 
|---|
| 131 | specific functions: | 
|---|
| 132 |  | 
|---|
| 133 | \snippet examples/xml/streambookmarks/xbelreader.h 1 | 
|---|
| 134 |  | 
|---|
| 135 | \dots | 
|---|
| 136 | \snippet examples/xml/streambookmarks/xbelreader.h 2 | 
|---|
| 137 | \dots | 
|---|
| 138 |  | 
|---|
| 139 | The \c read() function accepts a QIODevice and sets it with | 
|---|
| 140 | \l{QXmlStreamReader::setDevice()}{setDevice()}. The | 
|---|
| 141 | \l{QXmlStreamReader::raiseError()}{raiseError()} function is used to | 
|---|
| 142 | display a custom error message, inidicating that the file's version | 
|---|
| 143 | is incorrect. | 
|---|
| 144 |  | 
|---|
| 145 | \snippet examples/xml/streambookmarks/xbelreader.cpp 1 | 
|---|
| 146 |  | 
|---|
| 147 | The pendent to QXmlStreamReader is QXmlStreamWriter, which provides an XML | 
|---|
| 148 | writer with a simple streaming API. QXmlStreamWriter operates on a | 
|---|
| 149 | QIODevice and has specialised functions for all XML tokens or events you | 
|---|
| 150 | want to write, such as \l{QXmlStreamWriter::writeDTD()}{writeDTD()}, | 
|---|
| 151 | \l{QXmlStreamWriter::writeCharacters()}{writeCharacters()}, | 
|---|
| 152 | \l{QXmlStreamWriter::writeComment()}{writeComment()} and so on. | 
|---|
| 153 |  | 
|---|
| 154 | To write XML document with QXmlStreamWriter, you start a document with the | 
|---|
| 155 | \l{QXmlStreamWriter::writeStartDocument()}{writeStartDocument()} function | 
|---|
| 156 | and end it with \l{QXmlStreamWriter::writeEndDocument()} | 
|---|
| 157 | {writeEndDocument()}, which implicitly closes all remaining open tags. | 
|---|
| 158 | Element tags are opened with \l{QXmlStreamWriter::writeStartDocument()} | 
|---|
| 159 | {writeStartDocument()} and followed by | 
|---|
| 160 | \l{QXmlStreamWriter::writeAttribute()}{writeAttribute()} or | 
|---|
| 161 | \l{QXmlStreamWriter::writeAttributes()}{writeAttributes()}, | 
|---|
| 162 | element content, and then \l{QXmlStreamWriter::writeEndDocument()} | 
|---|
| 163 | {writeEndDocument()}. Also, \l{QXmlStreamWriter::writeEmptyElement()} | 
|---|
| 164 | {writeEmptyElement()} can be used to write empty elements. | 
|---|
| 165 |  | 
|---|
| 166 | Element content comprises characters, entity references or nested elements. | 
|---|
| 167 | Content can be written with \l{QXmlStreamWriter::writeCharacters()} | 
|---|
| 168 | {writeCharacters()}, a function that also takes care of escaping all | 
|---|
| 169 | forbidden characters and character sequences, | 
|---|
| 170 | \l{QXmlStreamWriter::writeEntityReference()}{writeEntityReference()}, | 
|---|
| 171 | or subsequent calls to \l{QXmlStreamWriter::writeStartElement()} | 
|---|
| 172 | {writeStartElement()}. | 
|---|
| 173 |  | 
|---|
| 174 | The \c XbelWriter class from \l{QXmlStream Bookmarks Example} is a subclass | 
|---|
| 175 | of QXmlStreamWriter. Its \c writeFile() function illustrates the core | 
|---|
| 176 | functions of QXmlStreamWriter mentioned above: | 
|---|
| 177 |  | 
|---|
| 178 | \snippet examples/xml/streambookmarks/xbelwriter.cpp 1 | 
|---|
| 179 |  | 
|---|
| 180 | \section1 The Qt SAX2 Classes | 
|---|
| 181 |  | 
|---|
| 182 | \section2 Introduction to SAX2 | 
|---|
| 183 |  | 
|---|
| 184 | The SAX2 interface is an event-driven mechanism to provide the user with | 
|---|
| 185 | document information. An "event" in this context means something | 
|---|
| 186 | reported by the parser, for example, it has encountered a start tag, | 
|---|
| 187 | or an end tag, etc. | 
|---|
| 188 |  | 
|---|
| 189 | To make it less abstract consider the following example: | 
|---|
| 190 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 3 | 
|---|
| 191 |  | 
|---|
| 192 | Whilst reading (a SAX2 parser is usually referred to as "reader") | 
|---|
| 193 | the above document three events would be triggered: | 
|---|
| 194 | \list 1 | 
|---|
| 195 | \o A start tag occurs (\c{<quote>}). | 
|---|
| 196 | \o Character data (i.e. text) is found, "A quotation.". | 
|---|
| 197 | \o An end tag is parsed (\c{</quote>}). | 
|---|
| 198 | \endlist | 
|---|
| 199 |  | 
|---|
| 200 | Each time such an event occurs the parser reports it; you can set up | 
|---|
| 201 | event handlers to respond to these events. | 
|---|
| 202 |  | 
|---|
| 203 | Whilst this is a fast and simple approach to read XML documents, | 
|---|
| 204 | manipulation is difficult because data is not stored, simply handled | 
|---|
| 205 | and discarded serially. The \link #dom DOM interface | 
|---|
| 206 | \endlink reads in and stores the whole document in a tree structure; | 
|---|
| 207 | this takes more memory, but makes it easier to manipulate the | 
|---|
| 208 | document's structure.. | 
|---|
| 209 |  | 
|---|
| 210 | The Qt XML module provides an abstract class, \l QXmlReader, that | 
|---|
| 211 | defines the interface for potential SAX2 readers. Qt includes a reader | 
|---|
| 212 | implementation, \l QXmlSimpleReader, that is easy to adapt through | 
|---|
| 213 | subclassing. | 
|---|
| 214 |  | 
|---|
| 215 | The reader reports parsing events through special handler classes: | 
|---|
| 216 | \table | 
|---|
| 217 | \header \o Handler class \o Description | 
|---|
| 218 | \row \o \l QXmlContentHandler | 
|---|
| 219 | \o Reports events related to the content of a document (e.g. the start tag | 
|---|
| 220 | or characters). | 
|---|
| 221 | \row \o \l QXmlDTDHandler | 
|---|
| 222 | \o Reports events related to the DTD (e.g. notation declarations). | 
|---|
| 223 | \row \o \l QXmlErrorHandler | 
|---|
| 224 | \o Reports errors or warnings that occurred during parsing. | 
|---|
| 225 | \row \o \l QXmlEntityResolver | 
|---|
| 226 | \o Reports external entities during parsing and allows users to resolve | 
|---|
| 227 | external entities themselves instead of leaving it to the reader. | 
|---|
| 228 | \row \o \l QXmlDeclHandler | 
|---|
| 229 | \o Reports further DTD related events (e.g. attribute declarations). | 
|---|
| 230 | \row \o \l QXmlLexicalHandler | 
|---|
| 231 | \o Reports events related to the lexical structure of the | 
|---|
| 232 | document (the beginning of the DTD, comments etc.). | 
|---|
| 233 | \endtable | 
|---|
| 234 |  | 
|---|
| 235 | These classes are abstract classes describing the interface. The \l | 
|---|
| 236 | QXmlDefaultHandler class provides a "do nothing" default | 
|---|
| 237 | implementation for all of them. Therefore users only need to overload | 
|---|
| 238 | the QXmlDefaultHandler functions they are interested in. | 
|---|
| 239 |  | 
|---|
| 240 | To read input XML data a special class \l QXmlInputSource is used. | 
|---|
| 241 |  | 
|---|
| 242 | Apart from those already mentioned, the following SAX2 support classes | 
|---|
| 243 | provide additional useful functionality: | 
|---|
| 244 | \table | 
|---|
| 245 | \header \o Class \o Description | 
|---|
| 246 | \row \o \l QXmlAttributes | 
|---|
| 247 | \o Used to pass attributes in a start element event. | 
|---|
| 248 | \row \o \l QXmlLocator | 
|---|
| 249 | \o Used to obtain the actual parsing position of an event. | 
|---|
| 250 | \row \o \l QXmlNamespaceSupport | 
|---|
| 251 | \o Used to implement namespace support for a reader. Note that | 
|---|
| 252 | namespaces do not change the parsing behavior. They are only | 
|---|
| 253 | reported through the handler. | 
|---|
| 254 | \endtable | 
|---|
| 255 |  | 
|---|
| 256 | The \l{SAX Bookmarks example} illustrates how to subclass | 
|---|
| 257 | QXmlDefaultHandler to read an XML bookmark file (XBEL) and | 
|---|
| 258 | how to generate XML by hand. | 
|---|
| 259 |  | 
|---|
| 260 | \section2 SAX2 Features | 
|---|
| 261 |  | 
|---|
| 262 | The behavior of an XML reader depends on its support for certain | 
|---|
| 263 | optional features. For example, a reader may have the feature "report | 
|---|
| 264 | attributes used for namespace declarations and prefixes along with | 
|---|
| 265 | the local name of a tag". Like every other feature this has a unique | 
|---|
| 266 | name represented by a URI: it is called | 
|---|
| 267 | \e http://xml.org/sax/features/namespace-prefixes. | 
|---|
| 268 |  | 
|---|
| 269 | The Qt SAX2 implementation can report whether the reader has | 
|---|
| 270 | particular functionality using the QXmlReader::hasFeature() | 
|---|
| 271 | function. Available features can be tested with QXmlReader::feature(), | 
|---|
| 272 | and switched on or off using QXmlReader::setFeature(). | 
|---|
| 273 |  | 
|---|
| 274 | Consider the example | 
|---|
| 275 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 4 | 
|---|
| 276 | A reader that does not support the \e | 
|---|
| 277 | http://xml.org/sax/features/namespace-prefixes feature would report | 
|---|
| 278 | the element name \e document but not its attributes \e xmlns:book and | 
|---|
| 279 | \e xmlns with their values. A reader with the feature \e | 
|---|
| 280 | http://xml.org/sax/features/namespace-prefixes reports the namespace | 
|---|
| 281 | attributes if the \link QXmlReader::feature() feature\endlink is | 
|---|
| 282 | switched on. | 
|---|
| 283 |  | 
|---|
| 284 | Other features include \e http://xml.org/sax/features/namespace | 
|---|
| 285 | (namespace processing, implies \e | 
|---|
| 286 | http://xml.org/sax/features/namespace-prefixes) and \e | 
|---|
| 287 | http://xml.org/sax/features/validation (the ability to report | 
|---|
| 288 | validation errors). | 
|---|
| 289 |  | 
|---|
| 290 | Whilst SAX2 leaves it to the user to define and implement whatever | 
|---|
| 291 | features are required, support for \e | 
|---|
| 292 | http://xml.org/sax/features/namespace (and thus \e | 
|---|
| 293 | http://xml.org/sax/features/namespace-prefixes) is mandantory. | 
|---|
| 294 | The \l QXmlSimpleReader implementation of \l QXmlReader, | 
|---|
| 295 | supports them, and can do namespace processing. | 
|---|
| 296 |  | 
|---|
| 297 | \l QXmlSimpleReader is not validating, so it | 
|---|
| 298 | does not support \e http://xml.org/sax/features/validation. | 
|---|
| 299 |  | 
|---|
| 300 | \section2 Namespace Support via Features | 
|---|
| 301 |  | 
|---|
| 302 | As we have seen in the previous section, we can configure the | 
|---|
| 303 | behavior of the reader when it comes to namespace | 
|---|
| 304 | processing. This is done by setting and unsetting the | 
|---|
| 305 | \e http://xml.org/sax/features/namespaces and | 
|---|
| 306 | \e http://xml.org/sax/features/namespace-prefixes features. | 
|---|
| 307 |  | 
|---|
| 308 | They influence the reporting behavior in the following way: | 
|---|
| 309 | \list 1 | 
|---|
| 310 | \o Namespace prefixes and local parts of elements and attributes can | 
|---|
| 311 | be reported. | 
|---|
| 312 | \o The qualified names of elements and attributes are reported. | 
|---|
| 313 | \o \l QXmlContentHandler::startPrefixMapping() and \l | 
|---|
| 314 | QXmlContentHandler::endPrefixMapping() are called by the reader. | 
|---|
| 315 | \o Attributes that declare namespaces (i.e. the attribute \e xmlns and | 
|---|
| 316 | attributes starting with \e{xmlns:}) are reported. | 
|---|
| 317 | \endlist | 
|---|
| 318 |  | 
|---|
| 319 | Consider the following element: | 
|---|
| 320 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 5 | 
|---|
| 321 | With \e http://xml.org/sax/features/namespace-prefixes set to true | 
|---|
| 322 | the reader will report four attributes; but with the \e | 
|---|
| 323 | namespace-prefixes feature set to false only three, with the \e | 
|---|
| 324 | xmlns:fnord attribute defining a namespace being "invisible" to the | 
|---|
| 325 | reader. | 
|---|
| 326 |  | 
|---|
| 327 | The \e http://xml.org/sax/features/namespaces feature is responsible | 
|---|
| 328 | for reporting local names, namespace prefixes and URIs. With \e | 
|---|
| 329 | http://xml.org/sax/features/namespaces set to true the parser will | 
|---|
| 330 | report \e title as the local name of the \e fnord:title attribute, \e | 
|---|
| 331 | fnord being the namespace prefix and \e http://www.qtsoftware.com/fnord/ as | 
|---|
| 332 | the namespace URI. When \e http://xml.org/sax/features/namespaces is | 
|---|
| 333 | false none of them are reported. | 
|---|
| 334 |  | 
|---|
| 335 | In the current implementation the Qt XML classes follow the definition | 
|---|
| 336 | that the prefix \e xmlns itself isn't associated with any namespace at all | 
|---|
| 337 | (see \link http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-using | 
|---|
| 338 | http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-using \endlink). | 
|---|
| 339 | Therefore even with \e http://xml.org/sax/features/namespaces and | 
|---|
| 340 | \e http://xml.org/sax/features/namespace-prefixes both set to true | 
|---|
| 341 | the reader won't return either a local name, a namespace prefix or | 
|---|
| 342 | a namespace URI for \e xmlns:fnord. | 
|---|
| 343 |  | 
|---|
| 344 | This might be changed in the future following the W3C suggestion | 
|---|
| 345 | \link http://www.w3.org/2000/xmlns/ http://www.w3.org/2000/xmlns/ \endlink | 
|---|
| 346 | to associate \e xmlns with the namespace \e http://www.w3.org/2000/xmlns. | 
|---|
| 347 |  | 
|---|
| 348 | As the SAX2 standard suggests, \l QXmlSimpleReader defaults to having | 
|---|
| 349 | \e http://xml.org/sax/features/namespaces set to true and | 
|---|
| 350 | \e http://xml.org/sax/features/namespace-prefixes set to false. | 
|---|
| 351 | When changing this behavior using \l QXmlSimpleReader::setFeature() | 
|---|
| 352 | note that the combination of both features set to | 
|---|
| 353 | false is illegal. | 
|---|
| 354 |  | 
|---|
| 355 | \section3 Summary | 
|---|
| 356 |  | 
|---|
| 357 | \l QXmlSimpleReader implements the following behavior: | 
|---|
| 358 |  | 
|---|
| 359 | \table | 
|---|
| 360 | \header \o (namespaces, namespace-prefixes) | 
|---|
| 361 | \o Namespace prefix and local part | 
|---|
| 362 | \o Qualified names | 
|---|
| 363 | \o Prefix mapping | 
|---|
| 364 | \o xmlns attributes | 
|---|
| 365 | \row \o (true, false) \o Yes \o Yes* \o Yes \o No | 
|---|
| 366 | \row \o (true, true) \o Yes \o Yes \o Yes \o Yes | 
|---|
| 367 | \row \o (false, true) \o No* \o Yes \o No* \o Yes | 
|---|
| 368 | \row \o (false, false) \i41 Illegal | 
|---|
| 369 | \endtable | 
|---|
| 370 |  | 
|---|
| 371 | The behavior of the entries marked with an asterisk (*) is not specified by SAX. | 
|---|
| 372 |  | 
|---|
| 373 | \section2 Properties | 
|---|
| 374 |  | 
|---|
| 375 | Properties are a more general concept. They have a unique name, | 
|---|
| 376 | represented as an URI, but their value is \c void*. Thus nearly | 
|---|
| 377 | anything can be used as a property value. This concept involves some | 
|---|
| 378 | danger, though: there is no means of ensuring type-safety; the user | 
|---|
| 379 | must take care that they pass the right type. Properties are | 
|---|
| 380 | useful if a reader supports special handler classes. | 
|---|
| 381 |  | 
|---|
| 382 | The URIs used for features and properties often look like URLs, e.g. | 
|---|
| 383 | \c http://xml.org/sax/features/namespace. This does not mean that the | 
|---|
| 384 | data required is at this address. It is simply a way of defining | 
|---|
| 385 | unique names. | 
|---|
| 386 |  | 
|---|
| 387 | Anyone can define and use new SAX2 properties for their readers. | 
|---|
| 388 | Property support is not mandatory. | 
|---|
| 389 |  | 
|---|
| 390 | To set or query properties the following functions are provided: \l | 
|---|
| 391 | QXmlReader::setProperty(), \l QXmlReader::property() and \l | 
|---|
| 392 | QXmlReader::hasProperty(). | 
|---|
| 393 |  | 
|---|
| 394 |  | 
|---|
| 395 | \target dom | 
|---|
| 396 | \section1 The Qt DOM Classes | 
|---|
| 397 |  | 
|---|
| 398 | \target domIntro | 
|---|
| 399 | \section2 Introduction to DOM | 
|---|
| 400 |  | 
|---|
| 401 | DOM provides an interface to access and change the content and | 
|---|
| 402 | structure of an XML file. It makes a hierarchical view of the document | 
|---|
| 403 | (a tree view). Thus -- in contrast to the SAX2 interface -- an object | 
|---|
| 404 | model of the document is resident in memory after parsing which makes | 
|---|
| 405 | manipulation easy. | 
|---|
| 406 |  | 
|---|
| 407 | All DOM nodes in the document tree are subclasses of \l QDomNode. The | 
|---|
| 408 | document itself is represented as a \l QDomDocument object. | 
|---|
| 409 |  | 
|---|
| 410 | Here are the available node classes and their potential child classes: | 
|---|
| 411 |  | 
|---|
| 412 | \list | 
|---|
| 413 | \o \l QDomDocument: Possible children are | 
|---|
| 414 | \list | 
|---|
| 415 | \o \l QDomElement (at most one) | 
|---|
| 416 | \o \l QDomProcessingInstruction | 
|---|
| 417 | \o \l QDomComment | 
|---|
| 418 | \o \l QDomDocumentType | 
|---|
| 419 | \endlist | 
|---|
| 420 | \o \l QDomDocumentFragment: Possible children are | 
|---|
| 421 | \list | 
|---|
| 422 | \o \l QDomElement | 
|---|
| 423 | \o \l QDomProcessingInstruction | 
|---|
| 424 | \o \l QDomComment | 
|---|
| 425 | \o \l QDomText | 
|---|
| 426 | \o \l QDomCDATASection | 
|---|
| 427 | \o \l QDomEntityReference | 
|---|
| 428 | \endlist | 
|---|
| 429 | \o \l QDomDocumentType: No children | 
|---|
| 430 | \o \l QDomEntityReference: Possible children are | 
|---|
| 431 | \list | 
|---|
| 432 | \o \l QDomElement | 
|---|
| 433 | \o \l QDomProcessingInstruction | 
|---|
| 434 | \o \l QDomComment | 
|---|
| 435 | \o \l QDomText | 
|---|
| 436 | \o \l QDomCDATASection | 
|---|
| 437 | \o \l QDomEntityReference | 
|---|
| 438 | \endlist | 
|---|
| 439 | \o \l QDomElement: Possible children are | 
|---|
| 440 | \list | 
|---|
| 441 | \o \l QDomElement | 
|---|
| 442 | \o \l QDomText | 
|---|
| 443 | \o \l QDomComment | 
|---|
| 444 | \o \l QDomProcessingInstruction | 
|---|
| 445 | \o \l QDomCDATASection | 
|---|
| 446 | \o \l QDomEntityReference | 
|---|
| 447 | \endlist | 
|---|
| 448 | \o \l QDomAttr: Possible children are | 
|---|
| 449 | \list | 
|---|
| 450 | \o \l QDomText | 
|---|
| 451 | \o \l QDomEntityReference | 
|---|
| 452 | \endlist | 
|---|
| 453 | \o \l QDomProcessingInstruction: No children | 
|---|
| 454 | \o \l QDomComment: No children | 
|---|
| 455 | \o \l QDomText: No children | 
|---|
| 456 | \o \l QDomCDATASection: No children | 
|---|
| 457 | \o \l QDomEntity: Possible children are | 
|---|
| 458 | \list | 
|---|
| 459 | \o \l QDomElement | 
|---|
| 460 | \o \l QDomProcessingInstruction | 
|---|
| 461 | \o \l QDomComment | 
|---|
| 462 | \o \l QDomText | 
|---|
| 463 | \o \l QDomCDATASection | 
|---|
| 464 | \o \l QDomEntityReference | 
|---|
| 465 | \endlist | 
|---|
| 466 | \o \l QDomNotation: No children | 
|---|
| 467 | \endlist | 
|---|
| 468 |  | 
|---|
| 469 | With \l QDomNodeList and \l QDomNamedNodeMap two collection classes | 
|---|
| 470 | are provided: \l QDomNodeList is a list of nodes, | 
|---|
| 471 | and \l QDomNamedNodeMap is used to handle unordered sets of nodes | 
|---|
| 472 | (often used for attributes). | 
|---|
| 473 |  | 
|---|
| 474 | The \l QDomImplementation class allows the user to query features of the | 
|---|
| 475 | DOM implementation. | 
|---|
| 476 |  | 
|---|
| 477 | To get started please refer to the \l QDomDocument documentation. | 
|---|
| 478 | You might also want to take a look at the \l{DOM Bookmarks example}, | 
|---|
| 479 | which illustrates how to read and write an XML bookmark file (XBEL) | 
|---|
| 480 | using DOM. | 
|---|
| 481 |  | 
|---|
| 482 | \target namespaces | 
|---|
| 483 | \section1 An Introduction to Namespaces | 
|---|
| 484 |  | 
|---|
| 485 | Parts of the Qt XML module documentation assume that you are familiar | 
|---|
| 486 | with XML namespaces. Here we present a brief introduction; skip to | 
|---|
| 487 | \link #namespacesConventions Qt XML documentation conventions \endlink | 
|---|
| 488 | if you already know this material. | 
|---|
| 489 |  | 
|---|
| 490 | Namespaces are a concept introduced into XML to allow a more modular | 
|---|
| 491 | design. With their help data processing software can easily resolve | 
|---|
| 492 | naming conflicts in XML documents. | 
|---|
| 493 |  | 
|---|
| 494 | Consider the following example: | 
|---|
| 495 |  | 
|---|
| 496 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 6 | 
|---|
| 497 |  | 
|---|
| 498 | Here we find three different uses of the name \e title. If you wish to | 
|---|
| 499 | process this document you will encounter problems because each of the | 
|---|
| 500 | \e titles should be displayed in a different manner -- even though | 
|---|
| 501 | they have the same name. | 
|---|
| 502 |  | 
|---|
| 503 | The solution would be to have some means of identifying the first | 
|---|
| 504 | occurrence of \e title as the title of a book, i.e. to use the \e | 
|---|
| 505 | title element of a book namespace to distinguish it from, for example, | 
|---|
| 506 | the chapter title, e.g.: | 
|---|
| 507 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 7 | 
|---|
| 508 |  | 
|---|
| 509 | \e book in this case is a \e prefix denoting the namespace. | 
|---|
| 510 |  | 
|---|
| 511 | Before we can apply a namespace to element or attribute names we must | 
|---|
| 512 | declare it. | 
|---|
| 513 |  | 
|---|
| 514 | Namespaces are URIs like \e http://www.qtsoftware.com/fnord/book/. This | 
|---|
| 515 | does not mean that data must be available at this address; the URI is | 
|---|
| 516 | simply used to provide a unique name. | 
|---|
| 517 |  | 
|---|
| 518 | We declare namespaces in the same way as attributes; strictly speaking | 
|---|
| 519 | they \e are attributes. To make for example \e | 
|---|
| 520 | http://www.qtsoftware.com/fnord/ the document's default XML namespace \e | 
|---|
| 521 | xmlns we write | 
|---|
| 522 |  | 
|---|
| 523 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 8 | 
|---|
| 524 |  | 
|---|
| 525 | To distinguish the \e http://www.qtsoftware.com/fnord/book/ namespace from | 
|---|
| 526 | the default, we must supply it with a prefix: | 
|---|
| 527 |  | 
|---|
| 528 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 9 | 
|---|
| 529 |  | 
|---|
| 530 | A namespace that is declared like this can be applied to element and | 
|---|
| 531 | attribute names by prepending the appropriate prefix and a ":" | 
|---|
| 532 | delimiter. We have already seen this with the \e book:title element. | 
|---|
| 533 |  | 
|---|
| 534 | Element names without a prefix belong to the default namespace. This | 
|---|
| 535 | rule does not apply to attributes: an attribute without a prefix does | 
|---|
| 536 | not belong to any of the declared XML namespaces at all. Attributes | 
|---|
| 537 | always belong to the "traditional" namespace of the element in which | 
|---|
| 538 | they appear. A "traditional" namespace is not an XML namespace, it | 
|---|
| 539 | simply means that all attribute names belonging to one element must be | 
|---|
| 540 | different. Later we will see how to assign an XML namespace to an | 
|---|
| 541 | attribute. | 
|---|
| 542 |  | 
|---|
| 543 | Due to the fact that attributes without prefixes are not in any XML | 
|---|
| 544 | namespace there is no collision between the attribute \e title (that | 
|---|
| 545 | belongs to the \e author element) and for example the \e title element | 
|---|
| 546 | within a \e chapter. | 
|---|
| 547 |  | 
|---|
| 548 | Let's clarify this with an example: | 
|---|
| 549 | \snippet doc/src/snippets/code/doc_src_qtxml.qdoc 10 | 
|---|
| 550 |  | 
|---|
| 551 | Within the \e document element we have two namespaces declared. The | 
|---|
| 552 | default namespace \e http://www.qtsoftware.com/fnord/ applies to the \e | 
|---|
| 553 | book element, the \e chapter element, the appropriate \e title element | 
|---|
| 554 | and of course to \e document itself. | 
|---|
| 555 |  | 
|---|
| 556 | The \e book:author and \e book:title elements belong to the namespace | 
|---|
| 557 | with the URI \e http://www.qtsoftware.com/fnord/book/. | 
|---|
| 558 |  | 
|---|
| 559 | The two \e book:author attributes \e title and \e name have no XML | 
|---|
| 560 | namespace assigned. They are only members of the "traditional" | 
|---|
| 561 | namespace of the element \e book:author, meaning that for example two | 
|---|
| 562 | \e title attributes in \e book:author are forbidden. | 
|---|
| 563 |  | 
|---|
| 564 | In the above example we circumvent the last rule by adding a \e title | 
|---|
| 565 | attribute from the \e http://www.qtsoftware.com/fnord/ namespace to \e | 
|---|
| 566 | book:author: the \e fnord:title comes from the namespace with the | 
|---|
| 567 | prefix \e fnord that is declared in the \e book:author element. | 
|---|
| 568 |  | 
|---|
| 569 | Clearly the \e fnord namespace has the same namespace URI as the | 
|---|
| 570 | default namespace. So why didn't we simply use the default namespace | 
|---|
| 571 | we'd already declared? The answer is quite complex: | 
|---|
| 572 | \list | 
|---|
| 573 | \o attributes without a prefix don't belong to any XML namespace at | 
|---|
| 574 | all, not even to the default namespace; | 
|---|
| 575 | \o additionally omitting the prefix would lead to a \e title-title clash; | 
|---|
| 576 | \o writing it as \e xmlns:title would declare a new namespace with the | 
|---|
| 577 | prefix \e title instead of applying the default \e xmlns namespace. | 
|---|
| 578 | \endlist | 
|---|
| 579 |  | 
|---|
| 580 | With the Qt XML classes elements and attributes can be accessed in two | 
|---|
| 581 | ways: either by refering to their qualified names consisting of the | 
|---|
| 582 | namespace prefix and the "real" name (or \e local name) or by the | 
|---|
| 583 | combination of local name and namespace URI. | 
|---|
| 584 |  | 
|---|
| 585 | More information on XML namespaces can be found at | 
|---|
| 586 | \l http://www.w3.org/TR/REC-xml-names/. | 
|---|
| 587 |  | 
|---|
| 588 |  | 
|---|
| 589 | \target namespacesConventions | 
|---|
| 590 | \section2 Conventions Used in the Qt XML Documentation | 
|---|
| 591 |  | 
|---|
| 592 | The following terms are used to distinguish the parts of names within | 
|---|
| 593 | the context of namespaces: | 
|---|
| 594 | \list | 
|---|
| 595 | \o  The \e {qualified name} | 
|---|
| 596 | is the name as it appears in the document. (In the above example \e | 
|---|
| 597 | book:title is a qualified name.) | 
|---|
| 598 | \o  A \e {namespace prefix} in a qualified name | 
|---|
| 599 | is the part to the left of the ":". (\e book is the namespace prefix in | 
|---|
| 600 | \e book:title.) | 
|---|
| 601 | \o  The \e {local part} of a name (also refered to as the \e {local | 
|---|
| 602 | name}) appears to the right of the ":". (Thus \e title is the | 
|---|
| 603 | local part of \e book:title.) | 
|---|
| 604 | \o  The \e {namespace URI} ("Uniform Resource Identifier") is a unique | 
|---|
| 605 | identifier for a namespace. It looks like a URL | 
|---|
| 606 | (e.g. \e http://www.qtsoftware.com/fnord/ ) but does not require | 
|---|
| 607 | data to be accessible by the given protocol at the named address. | 
|---|
| 608 | \endlist | 
|---|
| 609 |  | 
|---|
| 610 | Elements without a ":" (like \e chapter in the example) do not have a | 
|---|
| 611 | namespace prefix. In this case the local part and the qualified name | 
|---|
| 612 | are identical (i.e. \e chapter). | 
|---|
| 613 |  | 
|---|
| 614 | \sa {DOM Bookmarks Example}, {SAX Bookmarks Example} | 
|---|
| 615 | */ | 
|---|