将 XmlHttpRequest 解析为 XmlListModel [英] Parse XmlHttpRequest to XmlListModel

查看:43
本文介绍了将 XmlHttpRequest 解析为 XmlListModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将检索到的 xmlHttpRequest 对象放入 XMLListModel.我正在使用 qml.主要目标是评估我得到的 xml 并在列表中显示条目.如果有更好的方法 - 请告诉我.

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.

我找到了一个解决方案"在这里分析xml:http://developer.nokia.com/Community/Discussion/showthread.php/232839-Qt-Quick-and-DOM-doc-responseXML-returns-null在这里从 XMLHttpRequest 解析 XML但是深入挖掘 xml 结构非常糟糕,因为 xml 树的每一层都有循环.

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:

这将是我的最爱:将我从 xmlHttpRequest 获取的数据解析为 XmlList 内容并免费(自动)获取列表.这家伙也想要同样的,但没有写出解决方案:http://qt-project.org/forums/viewthread/6460

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

我也试过:

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

分别是第一个和最后一行,我在其中获取 xml.这表示错误:无法将空值分配给 QString".我确信,我得到了正确的 xml 答案,因为上面提到的搜索每个孩子和标记名的方法正在起作用.我还发现了一个不同的符号,比如解析器,但这也不起作用.

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.

var doc = new DOMParser().parseFromString(response, "text/xml");返回未定义的 DOMParser .. 所以我想我在那里需要一些库,但没有找到有关该主题的任何信息(除了未回答的问题).(与 .getElementById 和 evaluateXPath 以及我在网上找到的许多其他东西相同)

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)

感谢任何提示!

推荐答案

XmlListModelxml 属性必须是字符串类型.因此,您必须分配 xhr.responseText 而不是 xhr.responseXML.这是一个最小的工作示例(使用数据 URI 来模拟服务器响应):

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();
    }
}

这篇关于将 XmlHttpRequest 解析为 XmlListModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆