XmlListModel QML Type | Qt XML Patterns 5.15.1

- Qt 5.15
- Qt XML Patterns
- Qt 5.15.1 Reference Documentation

# XmlListModel QML Type

For specifying a read-only model using XPath expressions. More...

Import Statement: | import QtQuick.XmlListModel 2.15

- List of all members, including inherited members

## Properties

- count : int
- namespaceDeclarations : string
- progress : real
- query : string
- roles : list<XmlRole>
- source : url
- status : enumeration
- xml : string

## Methods

- errorString()
- object get(int index)
- reload()

## Detailed Description

To use this element, you will need to import the module with the following line:

     import QtQuick.XmlListModel 2.0

XmlListModel is used to create a read-only model from XML data. It can be used as a data source for view elements (such as ListView, PathView, GridView) and other elements that interact with model data (such as Repeater).

For example, if there is a XML document at http://www.mysite.com/feed.xml like this:

     <?xml version="1.0" encoding="utf-8"?>
     <rss version="2.0">
         ...
         <channel>
             <item>
                 <title>A blog post</title>
                 <pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
             </item>
             <item>
                 <title>Another blog post</title>
                 <pubDate>Sat, 07 Sep 2010 15:35:01 GMT</pubDate>
             </item>
         </channel>
     </rss>

A XmlListModel could create a model from this data, like this:

     import QtQuick 2.0
     import QtQuick.XmlListModel 2.0

     XmlListModel {
         id: xmlModel
         source: "http://www.mysite.com/feed.xml"
         query: "/rss/channel/item"

         XmlRole { name: "title"; query: "title/string()" }
         XmlRole { name: "pubDate"; query: "pubDate/string()" }
     }

The query value of "/rss/channel/item" specifies that the XmlListModel should generate a model item for each `<item>` in the XML document.

The XmlRole objects define the model item attributes. Here, each model item will have `title` and `pubDate` attributes that match the `title` and `pubDate` values of its corresponding `<item>`. (See XmlRole::query for more examples of valid XPath expressions for XmlRole.)

The model could be used in a ListView, like this:

     ListView {
         width: 180; height: 300
         model: xmlModel
         delegate: Text { text: title + ": " + pubDate }
     }

The XmlListModel data is loaded asynchronously, and status is set to `XmlListModel.Ready` when loading is complete. Note this means when XmlListModel is used for a view, the view is not populated until the model is loaded.

### Using Key XML Roles

You can define certain roles as "keys" so that when reload() is called, the model will only add and refresh data that contains new values for these keys.

For example, if above role for "pubDate" was defined like this instead:

     XmlRole { name: "pubDate"; query: "pubDate/string()"; isKey: true }

Then when reload() is called, the model will only add and reload items with a "pubDate" value that is not already present in the model.

This is useful when displaying the contents of XML documents that are incrementally updated (such as RSS feeds) to avoid repainting the entire contents of a model in a view.

If multiple key roles are specified, the model only adds and reload items with a combined value of all key roles that is not already present in the model.

See also Qt Quick Demo - RSS News.

## Property Documentation

count : int

The number of data entries in the model.

namespaceDeclarations : string

The namespace declarations to be used in the XPath queries.

The namespaces should be declared as in XQuery. For example, if a requested document at http://mysite.com/feed.xml uses the namespace "http://www.w3.org/2005/Atom", this can be declared as the default namespace:

     XmlListModel {
         source: "http://mysite.com/feed.xml"
         query: "/feed/entry"
         namespaceDeclarations: "declare default element namespace 'http://www.w3.org/2005/Atom';"

         XmlRole { name: "title"; query: "title/string()" }
     }

progress : real

This indicates the current progress of the downloading of the XML data source. This value ranges from 0.0 (no data downloaded) to 1.0 (all data downloaded). If the XML data is not from a remote source, the progress becomes 1.0 as soon as the data is read.

Note that when the progress is 1.0, the XML data has been downloaded, but it is yet to be loaded into the model at this point. Use the status property to find out when the XML data has been read and loaded into the model.

See also status and source.

query : string

An absolute XPath query representing the base query for creating model items from this model's XmlRole objects. The query should start with '/' or '//'.

roles : list<XmlRole>

The roles to make available for this model.

source : url

The location of the XML data source.

If both `source` and xml are set, xml is used.

status : enumeration

Specifies the model loading status, which can be one of the following:

- XmlListModel.Null - No XML data has been set for this model.
- XmlListModel.Ready - The XML data has been loaded into the model.
- XmlListModel.Loading - The model is in the process of reading and loading XML data.
- XmlListModel.Error - An error occurred while the model was loading. See errorString() for details about the error.

See also progress.

xml : string

This property holds the XML data for this model, if set.

The text is assumed to be UTF-8 encoded.

If both source and `xml` are set, `xml` is used.

## Method Documentation

errorString()

Returns a string description of the last error that occurred if status is XmlListModel::Error.

object get(int index)

Returns the item at index in the model.

For example, for a model like this:

     XmlListModel {
         id: model
         source: "http://mysite.com/feed.xml"
         query: "/feed/entry"
         XmlRole { name: "title"; query: "title/string()" }
     }

This will access the `title` value for the first item in the model:

     var title = model.get(0).title;

reload()

Reloads the model.

If no key roles have been specified, all existing model data is removed, and the model is rebuilt from scratch.

Otherwise, items are only added if the model does not already contain items with matching key role values.

See also Using key XML roles and XmlRole::isKey.