qtqmlsailfish-os

QT XmlListModel and Listview viewing data


I am trying to parse xml and view it in program:

<list>
    <item>
    This i want
    </item>
    <item>
    This i want 2 </item>
    <item>This i want</item>
    <item>This i want</item>
    <item>This i want</item>
    <item>This i want</item>
    <item/>
    <item/>
    <item/>
    </list>

With XmlListModel like this and view with ListModel:

XmlListModel {
            id: itemmodel
            source: page.dataURL
            query: "/list"

            XmlRole {
                name: "item"
                query: "item/string()"
            }
            }
  SilicaListView {
       width: 180; height: 300
       model: itemmodel
       delegate: Text { text: item }
   }

When I run this it Qt creator says: "Unable to assign [undefined] to QString". What I am doing wrong? I am making app to Sailfish and this silicalistview is same as listview in qt. My goal is to view them like this:

This I want
This I want 2

and so on.

Thank you for your time!


Solution

  • The issue I think is that you only have one <list> element and then all your <item> are in the same parent. So your query is looking for each <list> in your XML (only one) and then it prints only the first <item> element.

    Here is some code changing the XmlRole and the query that should work. I put the XML directly into the xml property to simplify:

    XmlListModel {
      id: itemmodel
      xml: "<list><item>This i want</item><item>This i want2</item></list>"
      query: "/list/item"
      XmlRole {
          name: "item"
          query: "string()"
      }
    }
    ListView {
       width: 180; height: 300
       model: itemmodel
       delegate: Rectangle {
         height: 40
         width: 100
         color:"red"
         Text {
           text: item
         }
       }
     }