Qt 4.8
XmlListModel Class Reference

The XmlListModel element is used to specify a read-only model using XPath expressions. More...

Detailed Description

The XmlListModel element is used to specify a read-only model using XPath expressions.

Since
4.7

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 1.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 XmlListModel::query{query} value of "/rss/channel/item" specifies that the XmlListModel should generate a model item for each 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 . (See XmlRole::query for more examples of valid XPath expressions for XmlRole.)

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

width: 180; height: 300
model: xmlModel
delegate: Text { text: title + ": " + pubDate }
}
qml-xmllistmodel-example.png

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
{RSS News}

The documentation for this class was generated from the following file: