The SAX interface |
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.
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://example.com/fnord/book/' xmlns = 'http://example.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.
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:
Consider the following element:
<author xmlns:fnord = 'http://example.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://example.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.
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.
Properties 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. Properties 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().
[Previous: XML Streaming] [XML Processing] [Next: Working with the DOM Tree]
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.6-snapshot | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com