- Qt 6.10
- QML Types
- XmlListModelRole
- Qt 6.10.2 Reference Documentation

# XmlListModelRole QML Type

For specifying a role to an XmlListModel. More...

Import Statement: | `import QtQml.XmlListModel`

- List of all members, including inherited members

## Properties

- attributeName : string
- elementName : string
- name : string

## Detailed Description

See also Qt Qml.

## Property Documentation

attributeName : string

The attribute of the XML element that will be used to read the data. The XML element is specified by elementName property.

For example, the following model has a role named "title", which reads the data from the XML element `<title>`. It also has another role named "timestamp", which uses the same XML element `<title>`, but reads its "created" attribute to extract the actual value.

     XmlListModel {
         id: xmlModel
         source: "file.xml"
         query: "/documents/document"
         XmlListModelRole { name: "title"; elementName: "title" }
         XmlListModelRole {
             name: "timestamp"
             elementName: "title"
             attributeName: "created"
         }
     }

     ListView {
         anchors.fill: parent
         model: xmlModel
         delegate: Text { text: title + " created on " + timestamp }
     }

When the attributeName is specified, the elementName can be left empty. In this case the attribute of the top level XML element of the query will be read.

For example, if you have the following xml document:

     <documents>
         <document title="Title1"/>
         <document title="Title2"/>
     </documents>

To extract the document titles you need the following model:

     XmlListModel {
         id: xmlModel
         source: "file.xml"
         query: "/documents/document"
         XmlListModelRole {
             name: "title"
             elementName: ""
             attributeName: "title"
         }
     }

If you do not need to parse any attributes for the specified XML element, simply leave this property blank.

See also elementName.

elementName : string

The name of the XML element, or a path to the XML element, that will be used to read the data. The element must actually contain text.

Optionally the attributeName property can be specified to extract the data.

For example, the following model has a role named "title", which reads the data from the XML element `<title>`. It also has another role named "timestamp", which uses the same XML element `<title>`, but reads its "created" attribute to extract the actual value.

     XmlListModel {
         id: xmlModel
         source: "file.xml"
         query: "/documents/document"
         XmlListModelRole { name: "title"; elementName: "title" }
         XmlListModelRole {
             name: "timestamp"
             elementName: "title"
             attributeName: "created"
         }
     }

     ListView {
         anchors.fill: parent
         model: xmlModel
         delegate: Text { text: title + " created on " + timestamp }
     }

When the attributeName is specified, the elementName can be left empty. In this case the attribute of the top level XML element of the query will be read.

For example, if you have the following xml document:

     <documents>
         <document title="Title1"/>
         <document title="Title2"/>
     </documents>

To extract the document titles you need the following model:

     XmlListModel {
         id: xmlModel
         source: "file.xml"
         query: "/documents/document"
         XmlListModelRole {
             name: "title"
             elementName: ""
             attributeName: "title"
         }
     }

The elementName property can actually contain a path to the nested xml element. All the elements in the path must be joined with the `'/'` character.

For example, if you have the following xml document:

     <documents>
         <document>
             <title>Title1</title>
             <info>
                 <num_pages>10</num_pages>
             </info>
         </document>
         <document>
             <title>Title2</title>
             <info>
                 <num_pages>20</num_pages>
             </info>
         </document>
     </documents>

You can extract the number of pages with the following role:

     XmlListModel {
         id: xmlModel
         source: "file.xml"
         query: "/documents/document"
         // ...
         XmlListModelRole {
             name: "pages"
             elementName: "info/num_pages"
         }
     }

Note: The path to the element must not start or end with `'/'`.

See also attributeName.

name : string

The name for the role. This name is used to access the model data for this role.

For example, the following model has a role named "title", which can be accessed from the view's delegate:

     XmlListModel {
         id: xmlModel
         source: "file.xml"
         query: "/documents/document"
         XmlListModelRole { name: "title"; elementName: "title" }
     }

     ListView {
         model: xmlModel
         delegate: Text { text: title }
     }