Important: Please read the Qt Code of Conduct -

XmlListModel - Getting XML data

  • Hi all,

    I've got a XmlListModel+ListView parsing and visualizing some data. But this data consists of multiple levels (e.g. it contains multiple "connection" elements, each containing multiple "vehicle" elements), and since QML doesn't provide a TreeView of some sort I want to visualize the second level of items in another page.

    But I can't manage to pass the XML from the first page to the second one, where it would get parsed a second time (with a query pointing to one of the "connection" elements). I would like to do this in order to prevent having to redownload the data.

    I've tried accessing the "xml" property of the model, but having set the URL through the "source" property it seems that I can't read the received XML back out (gives me an empty string). Maybe there exists an XPath function which would allow me to dump the XML contents of a single item into a property (using a XmlRole)? Or is there another way to accomplish this?


  • I don't think you can do it this way, but what I did in the past, is to fix the issue on the other side: by preventing the download to happen multiple times. Instead, I created a simple class that I exposed as an object in QML. This object exposed a URL property and a read-only text property. Set the URL property, and the text property will be updated once the data is in. This property can then be bound to the model's xml property.

    So, a little bit of C++ was needed for this solution. However, that was about a year ago, no idea if it is still needed.

  • Hmm, at first I didn't like circumventing the XmlListModel its download capabilities, but inspired by your comment (as well as some information in QTBUG-13688) I managed to download the XML source in a really compact way:
    @var source = "the source you would have used"

    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
    if (doc.readyState === XMLHttpRequest.DONE) {
    yourModel.xml = doc.responseText
    yourStatusWidget.text = "Loading...""GET", source);

    Although it would be nice to get a proper solution (it's strange and undocumented that the xml property doesn't contain the actual document contents after having used the source property) or even a hierarchical ListView (strange decision, leaving out a TreeView); but in the meanwhile this is a compact and easy-to-revert workaround.

Log in to reply