使用 qml 读取多个 Xml 属性
Reading multiple Xml Attributes with qml
I've got and XML String in javascript (qml(.我的目标是过滤有关不同行的信息。我想要一个包含行名(属性(的对象,尤其是倒计时。对于一个行名,有 departures_count * 倒计时字段。我希望所有这些(在当前情况下为 2(倒计时值在一个数组中。最终目标是将整行加载到以下形式的列表模型中:line(name, countdown(1,2,..x))
。
最大的问题是访问属性。在qml中,不知何故不支持DOM树的标准函数:"对象没有getAttribute((这样的函数"和其他函数,如getElementByTagName((。使用 XmlListModel,我可以访问属性,但前提是只有一个属性。在其他情况下,它返回未知(据我发现,qt 中有一个错误(。
我都尝试了纯XmlListModel,但没有运气(参见:Parse XmlHttpRequest to XmlListModel( - 不支持多个条目。所以我试着找一个工作轮:
要处理的 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 正则表达式
所以我的最后一个赌注是使用正则表达式。有没有办法获取所有倒计时值?这是我最好的尝试,但它不知何故只得到一个值(我尝试了最后的 + 来查找所有倒计时,但它不起作用。 /delay'='"'d*'".countdown'='"('d*)'"+/
而这个 for(var i = 0; i<5; i++) console.debug(found[i]);
是我检索比赛的方式。第二次迭代,所以发现[1]给了我1个正确的倒计时。但是我如何扩展这个概念以获得所有倒计时?
搜索并玩了 2 天后,我明白了。我希望这不会再打扰任何人了。我找到了两种可能的方法:
1 个循环循环
或者向上的DomTree(组织问题中的第1位(:
http://doc.qt.digia.com/qt-maemo/qdeclarativeglobalobject.html
这边是我一直在寻找的。它的文档很糟糕(就像我发现的关于 qml/qt 的所有内容一样(,但有一个需要读取属性字段的属性:属性(注意链接中的拼写错误(。它是一个属性数组,所以属性[1].name 是第二个属性的名称。
此后,我只需要一种简单的方法来爬树,这在组织问题中有描述。
2 Xml列表模型
由于存在分差计数,因此可以获取所有这些计数,然后以某种方式将倒计时(全部在列表中(和相应的行(包含一行有多少计数的信息(放在一起。
现在这只是数字 2,因为如果您没有提示每个节点有多少属性,这将不起作用。订单会丢失。
XmlListModel {
id: lineXmlModel
query: "/ft/response/monitor/lines/line"
onStatusChanged: {
if (status === 1) {
console.debug("lineModel has: "+count+" items");
for (var i = 0; i < count; i++) {
lineListModel.append({"line": get(i).line});// , "depaturesCount": parseFloat(get(i).depaturesCount) });
}
}
}
// name = variable name, query fetches the data with XPATH
XmlRole { name: "line"; query: "@name/string()" }
} // xmlModel
XmlListModel {
id: depatureCountXmlModel
query: "/ft/response/monitor/lines/line/departures"
onStatusChanged: {
if (status === 1) {
console.debug("departureCountModel has: "+count+" items");
for (var i = 0; i < count; i++) {
lineListModel.append({"line": get(i).line, "depatureCount": parseFloat(get(i).depaturesCount) });
}
}
}
XmlRole { name: "depatureCount"; query: "@count/number()" }
} // xmlModel
XmlListModel {
id: countdownXmlModel
query: "/ft/response/monitor/lines/line/departures/departure/departureTime"
onStatusChanged: {
if (status === 1 && lineXmlModel.status === 1 && depatureCountXmlModel.status === 1) {
console.debug("CountdownModel has: "+count+" items");
for (var i = 0; i < lineXmlModel.count; i++) {
console.debug("Line: "+ lineXmlModel.get(i).name + " number of depatures "+ depatureCountXmlModel.get(i).depatureCount );
// console.debug("countdown "+ parseFloat(get(i).countdown) );
// lineListModel.append({"countdown": parseFloat(get(i).countdown) });
}
}
}
XmlRole { name: "countdown"; query: "@countdown/number()" }
}
这是否能解决你的问题。但是我有类似的问题,并像这样解决了它(与您的问题有关(。`
import QtQuick 2.0
import QtQuick.XmlListModel 2.0
ListView
{
width: 300;
height: 400;
spacing: 10
model: XmlListModel
{
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="1" countdown="3"/>
</departure>
<departure>
<departureTime delay="2" countdown="6"/>
</departure>
<firstDeparture>
<departureTime delay="3" countdown="99"/>
</firstDeparture>
<lastDeparture>
<departureTime delay="4" countdown="98"/>
</lastDeparture>
</departures>
</line>
</lines>
</monitor>
<trafficInfos/>
<message messageCode="1">ok</message>
</response>
</ft>';
query:"/ft/response/monitor/lines/line/departures/departure"
XmlRole { name:"delay" ; query: "departureTime/@delay/number()" }
XmlRole { name:"countdown" ; query: "departureTime/@countdown/number()" }
}
delegate:
Rectangle
{
color: "pink";
width:parent.width;
height: col.height
Column
{
id: col
Text{ text: countdown }
Text{ text: delay }
}
}
}// listView `
- LiveCycle Xml属性绑定
- 使用javascript查找xml属性值
- for循环,直到xml属性不为null.从最后一行开始javascript
- 无法使用 jquery 获取 xml 属性的值
- 使用 qml 读取多个 Xml 属性
- 在 HTML 页面中显示 XML 属性
- 更改XML文件中的XML属性值
- 在“跨记录”中查找XML属性的最大值
- 读取xml子节点jquery上的多个xml属性
- 使用正则表达式替换XML属性内错误字符串中的嵌套引号
- js查找具有另一个属性的xml属性
- XML属性中的Dojo网格值
- 基于XML属性更改样式的indesigncs6脚本不工作
- Jquery获取XML属性值,用转义不工作
- 使用javascript解析XML属性值
- javascript将XML属性推入数组
- 使用变量名查找XML属性值
- 使用javascript xpath检查带有XML属性的字符串值
- 打开/关闭XML属性状态
- 如何使用jquery获得Xml属性?