Skip to content Skip to sidebar Skip to footer

Reading Multiple Xml Attributes With Qml

I've got and XML String in javascript (qml). My goal is to filter informations regarding different lines. I want an object containing the line name ( attribute) and especially the

Solution 1:

After searching and playing 2 days through, I got it. I hope this won't bother anyone anymore. There are 2 possible ways I found:

1 loopy loop

Or up the DomTree (number 1 in the org. question):

http://doc.qt.digia.com/qt-maemo/qdeclarativeglobalobject.html

This side was what I was looking for the whole time. It is bad documented (like everything I found about qml/qt), but has the one property one needs to read the attribute fields: attributes (note the typo in the link). It is an array of the attributes, so attributes[1].name is the name of the second att.

Thereafter I just need an easy way to climb the tree, which is described in the org. question.

2 XmlListModels

Since there is a depature count, one can fetch all these counts and then somehow fiddle together the countdowns (all in a list) and the respective lines (with the info how many counts for a line).

This is now just number 2, since if you don't got a hint how many attributes per node, this won't work. The order would go lost.

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

Solution 2:

Hi I do not know if that solves your problem. But I had similar problem and sovled it (related to your problem) like this. `

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><clientdevice=""appName=""clientId="123"appVersion=""/><responseType>api_get_monitor</responseType><responseTime>2011-05-31 14:41:13</responseTime><monitorid="36469"clientexpiration=""><linescount="24"><linename="U1"type="ptMetro"towards="Leopoldau"direction="H"platform="U1_H"barrierFree="1"realtimeSupported="1"><departurescount="2"><departure><departureTimedelay="1"countdown="3"/></departure><departure><departureTimedelay="2"countdown="6"/></departure><firstDeparture><departureTimedelay="3"countdown="99"/></firstDeparture><lastDeparture><departureTimedelay="4"countdown="98"/></lastDeparture></departures></line></lines></monitor><trafficInfos/><messagemessageCode="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 `

Post a Comment for "Reading Multiple Xml Attributes With Qml"