| Classes - Annotated - Tree - Functions - Home - Structure

# Qt XML Module

The XML module provides a well-formed XML parser using the SAX2 (Simple API for XML) interface plus an implementation of the DOM Level 2 (Document Object Model).

This document assumes that you are familiar with the essentials of XML. It provides you with information on the XML module in Qt and explains some often neglected XML features that will help you make the best use of the Qt XML classes.

We will however not teach XML basics. If you wish to learn more about XML please refer to other sources, e.g. http://www.w3.org/XML/.

## Overview of the XML architecture in Qt

The Qt XML Module provides two interfaces for XML: SAX2 and DOM Level 2.

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.megginson.com/SAX/.

Support for SAX2 filters and the reader factory are under development. Furthermore 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 implementation". A code example is discussed in the "tagreader walkthrough".

DOM Level 2 is a W3C Recommendation for XML interfaces that maps the constituents of an XML document to a tree structure. Details and the specification of DOM Level 2 can be found at http://www.w3.org/DOM/. More information about the DOM classes in Qt is provided in the Qt XML DOM overview.

## 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 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 occurence 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 have to 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.

Lets clarify matters 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, even not to the default namespace;
- additionaly 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 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).