我已经得到了,并在JavaScript(qml)中的XML字符串。我的目标是过滤关于不同线路的信息。我想要一个包含行名称(属性)的对象,尤其是倒计数。对于一个线路名称,有departures_count *倒计时字段。我希望所有这些(在当前情况下是2)数组中的倒数值。最终目标是将整行加载到以下格式的ListModel中:line(name, countdown(1,2,..x))
。用qml读取多个XML属性
最大的问题是访问属性。在qml中,不支持DOM树的标准函数:“对象没有getAttribute()”等其他函数,就像getElementByTagName()一样。有了XmlListModel,我可以访问属性,但只有一个。在其他情况下,它返回未知数(根据我的发现,qt中存在一个错误)。
我已经尝试过纯XmlListModel,但没有运气(请参阅:Parse XmlHttpRequest to XmlListModel) - 那里不支持多个条目。因此,我试图找到一个workarround:
要处理的XML:
<?xml version="1.0" encoding="UTF-8"?>
<ft>
<response>
<client device="" appName="" clientId="123" appVersion=""/>
<responseType>api_get_monitor</responseType>
<responseTime>2011-05-31 14:41:13</responseTime>
<monitor id="36469" clientexpiration="">
<lines count="24">
<line name="U1" type="ptMetro" towards="Leopoldau" direction="H" platform="U1_H" barrierFree="1" realtimeSupported="1">
<departures count="2">
<departure>
<departureTime delay="" countdown="3"/>
</departure>
<departure>
<departureTime delay="" countdown="6"/>
</departure>
<firstDeparture>
<departureTime delay="" countdown=""/>
</firstDeparture>
<lastDeparture>
<departureTime delay="" countdown=""/>
</lastDeparture>
</departures>
</line>
</lines>
</monitor>
<trafficInfos/>
<message messageCode="1">ok</message>
</response>
</ft>
1攀登对象XML树
随着
function getElementsByTagName(rootElement, tagName) {
var childNodes = rootElement.childNodes;
var elements = [];
for(var i = 0; i < childNodes.length; i++) {
if(childNodes[i].tagName === tagName) {
elements.push(childNodes[i]);
}
}
return elements;
}
我能挖得到元素排成整个xml树。
attributeInterface.xml = depatures[0];
attributeInterface.query = "/"
attributeInterface.roles.name = "countdown";
attributeInterface.roles.query = "@countdown/string()";
以及与此:
XmlListModel {
id: attributeInterface
onStatusChanged: {
for (var i = 0; i < count; i++) {
console.debug({"countdown": parseFloat(get(i).countdown) });
}}}
我试图让属性出来。但问题在于,赋值是无效的,因为xml元素是对象(DOM?但是这种方法不存在),而不是文本。
2正则表达式
所以我最后一个打赌就是使用正则表达式。有没有办法获得所有倒计时值?这是我最好的尝试,但它在某种程度上只是得到一个值(我试过+末找到所有的倒计时,但它wouldnt工作。/delay\=\"\d*\".countdown\=\"(\d*)\"+/
这 for(var i = 0; i<5; i++) console.debug(found[i]);
是我如何找回比赛。第二次迭代,所以找到[1]给了我1个正确的倒计时,但是如何扩展这个概念才能获得所有的倒计时?