2

I want to get my retrieved xmlHttpRequest object into an XMLListModel. I am using qml. The main goal is to evaluate the xml I get and show the entries in a list. If there's a better method - let me know.

I found a "solution" here for analyzing the xml: http://developer.nokia.com/Community/Discussion/showthread.php/232839-Qt-Quick-and-DOM-doc-responseXML-returns-null and here Parse XML from XMLHttpRequest But it is VERY poor to dig in deep xml structures, because there are loops arround every level of the xml tree.

So the 2 ways I would like to have:

1: XmlList

This would be my favourite: parse the data I got from the xmlHttpRequest to a XmlList thing and get the list for free (automatically). This guy wanted the same, but didn't write out a solution: http://qt-project.org/forums/viewthread/6460

I also tried:

XmlListModel{id: xmlModel}
...
xmlModel.xml = xhr.responseXML;

The first one separately, and the last line, where I get the xml. This says "Error: Cannot assign null to QString". I am sure, that I get a correct xml answer, because the above mentioned method with searching for each child and the tagname is working. Also I found a different notation with something like a parser, but that didn't work either.

2: XPath

var doc = new DOMParser().parseFromString(response, "text/xml"); returnes DOMParser not defined .. so I guess I would need some library there, but didn't find anything about the topic (else than unanswered questions). (Same with .getElementById and evaluateXPath and many other thing I found on the net)

Any hint is appreciated!

Community
  • 1
  • 1
mike
  • 791
  • 11
  • 26

1 Answers1

1

The xml property of XmlListModel must be of type string. Therefore you have to assign xhr.responseText instead of xhr.responseXML. Here is a minimal working example (using a data URI so simulate a server response):

import QtQuick 1.0

ListView {
    width: 200; height: 200

    delegate: Text {
        text: name 
    }

    model: XmlListModel {
        id: xmlModel
        query: "/names/name"

        XmlRole { name: "name"; query: "string()" }
    }

    Component.onCompleted: {
        /*  <names>
                <name>John</name>
                <name>Max</name>
                <name>Sandy</name>
            </names> */
        var dataURI = "data:application/xml;base64,PG5hbWVzPjxuYW1lPkpvaG48L25hbWU+PG5hbWU+TWF4PC9uYW1lPjxuYW1lPlNhbmR5PC9uYW1lPjwvbmFtZXM+"

        var req = new XMLHttpRequest();
        req.onreadystatechange = function () {
            if (req.readyState == 4) {
                xmlModel.xml = req.responseText; //<<<
            }
        };
        req.open("get", dataURI, true);
        req.send();
    }
}
hiddenbit
  • 2,233
  • 14
  • 25
  • Thanks, this solved it at first. But then I bumped to the boundary, that a query can't be nested or iterated. I would need an entry (i.e. a book) with more than one sub-entries (chapter-lengths). As far as I found. This is impossible. So I need to iterate in a hell mess of loops to get the data. Or do you know another way? I.e: using xpath without xmllistmodel? So a full solution could produce a book with an array, where the number of pages is stored. – mike Oct 09 '13 at 11:10
  • The error is: Required cardinality is zero or one("?"); got cardinality one or more("+"). – mike Oct 09 '13 at 11:13
  • You could create a `ListView` of `Repeater`s: The outer `ListView` has a `XmlListModel` for the books and each of its `Repeater` delegates contains a `XmlListModel` with a generated XPath expression to select the chapters of the respective book. The nested `XmlListModel`s of the `Reapeater`s use the XML data from the outer `XmlListModel` of the `ListView`. – hiddenbit Oct 09 '13 at 14:29
  • This would be my first choice, but I somewhere found, that the XmlListModel provides no support for multiple Attributes. The thing is, that I have 2-4 attributes for each element. So now I implemented the whole thing with loops and my own getElementByTagName() function. But the problem is, that there is no .getAttribute function... so the next workarround is an XmlListModel as Interface to the Attributes... But I guess this should go into another question, the whole Attribute thing is distant related to the org. question. – mike Oct 09 '13 at 19:42
  • For anyone interested: http://stackoverflow.com/questions/19281773/reading-multiple-xml-attributes-with-qml this is my question regarding the reading done. If there ain't just one, but many nodes with the same attribute the XmlRole wont work. – mike Oct 10 '13 at 19:22