XML data models

The XmlDataModel does not support adding or inserting values. For that reason, using an XmlDataModel in a list is only really useful for prototyping purposes. To be able to save XML data, you'll need to use either XmlDataAccess (for C++) or DataSource (for QML). For more information about how to use these classes, see Data.

You can use an XmlDataModel to create a data model that's based on an XML file. You can define data items as XML elements in the file, and create properties for these elements to represent the data itself. For example, you can define an XML element that represents a contact, and create properties such as name, address, and phoneNumber. To retrieve the data for an item, call  XmlDataModel::data() and specify the index path of that item as an argument.

One advantage of using an XmlDataModel is that you can create the visual representation for your list (by using a  ListView) and populate it with data entirely in QML. You don't need to use any C++ code to create the model, which makes using XmlDataModel a good approach for static data models or UI prototyping. XML is also easy to work with, and you can construct a complex data model quickly to use in your list.

Here's an example of an XML file called apps.xml that you could use as a data model. The file includes a mandatory root element called root (which is not displayed in a ListView), and several elements that define different categories of applications. Each element has a type of either appCategory (indicating that the item specifies a category of app) or appItem (indicating that the item specifies a particular app). Each item in the model includes a title property, and items with a type of appItem also have a version property.

// apps.xml
    <appCategory title="Productivity">
        <appItem title="Messages" version="1.2" />
        <appItem title="Contacts" version="2.0" />
        <appItem title="Calendar" version="1.0" />
    <appCategory title="Games">
        <appItem title="BrickBreaker" version="7.1" />
        <appItem title="Word Mole" version="7.1" />
    <appCategory title="Social networking">
        <appItem title="BlackBerry Messenger" version="6.1" />
        <appItem title="Twitter" version="2.0" />
        <appItem title="Facebook" version="2.0" />

To associate this data model with a ListView in QML, you can use the dataModel property and create an XmlDataModel based on this file. When you specify the XML file to use as the source, make sure that you use the location of the file relative to the location of your QML file. Assuming that the apps.xml file is located in the same folder as your QML file, you can use it as follows:

ListView {
    dataModel: XmlDataModel {
        source: "apps.xml"

To retrieve the data that's associated with an item in the model, you can call XmlDataModel::data() and specify the index path of the item you're interested in. You can access the properties of each item by name, and you can use the type of each item to create a custom appearance in your list. For more information about customizing the appearance of list items, see List view.

Here's how you could implement the onTriggered signal handler for the ListView in the code sample above. To learn more about responding to selection, see Responding to selection.

onTriggered: {
    var selectedItem = dataModel.data(indexPath);
    // Do something with the item that was tapped

Last modified: 2014-05-14

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus