Parse XmlHttpRequest to XmlListModel

有些话、适合烂在心里 提交于 2019-12-06 13:23:22

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!