Given the following XML source with 2 nested levels:

<papers>
  <paper id="1">
    <authors>
      <author name="John Doe" />
      <author name="Jane Doe" />
    </authors>
  </paper>
  <paper id="2">
    <authors>
      <author name="John Appleseed" />
    </authors>
  </paper>
</papers>

when I define an XmlRole in my XmlListModel like:

XmlListModel {
  id: myPapers
  source: "http://example.com/papers.xml"
  query: "/papers/paper"
  XmlRole { name: "author"; query: "authors/author/@name/string()" }
}

it will contain all 3 authors, not only the 2 "Doe" authors from the first paper. I can add an index (authors/author[1]) in the role but I don't now how many papers and authors there will be. Defining author1, author2, ..., author99 doesn't feel like the right approach.

How do I tell the XmlListModel I only want the authors of one paper in a ListView?

ListView {
  model: myPapers 
  delegate: Text { text: author }
}

Now shows:

John Doe
Jane Doe
John Appleseed <-- don't want this one

Thanks!

UPDATE 1 The source file is a URL and I prefer not to fetch the same file multiple times to get the data (it's for a mobile app).

UPDATE 2 upsideout on #qt-quick told me it's better to use a ListView instead of a Repeater. Don't understand why yet. Modified the question accordingly.

share|improve this question
2  
Never tried, but maybe it works if you add an index to the query? Similar to what is done here: stackoverflow.com/questions/1209382/… – danielfranca Mar 21 '15 at 23:52
    
Thanks @danielfranca, see my longer response below, it works... but... – Cimm Mar 22 '15 at 13:02
up vote 3 down vote accepted

Did some more digging and bugged some more people and the final answer is: no, this is not possible with a standard XmlListModel. You have to roll your own solution.

Here is a snippet of my solution for future references - implementation borrowed from Tim Besard's BeTrains.QML code:

Item {
    property bool loading: false
    property bool error: false
    property ListModel papers: ListModel{}
    property string source: "http://example.com/papers.xml"

    function fetch() {
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
            error = false;
            if (doc.readyState === XMLHttpRequest.DONE) {
                if (doc.status != 200) {
                    error = true;
                } else {
                    var rootNode = doc.responseXML.documentElement;
                    parsePapers(rootNode);
                }
                loading = false;
            }
        }
        loading = true;
        doc.open("GET", source);
        doc.send();
    }

    function parsePapers(rootNode) {
        papers.clear();
        for (var i=0; i < rootNode.childNodes.length; ++i) {
            var node = rootNode.childNodes[i];
            if (node.nodeName == "paper") {
                [snip]...[/snip]
            }
        }
    }
}

This solution is not only way more complex than the XmlListModel solution: It also seems that QML only has basic XML parser capabilities.

Finally, if the "multiple HTTP requests for the same resource" does not matter for you, you can go with the solution linked by @danielfranca.

share|improve this answer

Played with @danielfranca's solution by modifying the query of the XmlListModel in the ListView:

ListView {
  model: XmlListModel {
    query: "/paper/authors["+(index+1)+"]"
  }
  Text { text: author }
}

This works, but... in my use case the XML source is a URL meaning this will create another HTTP request for each list. This is unnecessary as the data is already there with the first request. Anyone a better solution?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.