Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Parsing XML with XmlListModel

Parsing XML with XmlListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 323 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by
    #1

    Hello,

    I need to parse XML like:

    <response version="1.2" xsi:noNamespaceSchemaLocation="http://aviationweather.gov/adds/schema/taf1_2.xsd">
    <request_index>19865992</request_index>
    <data_source name="tafs"/>
    <request type="retrieve"/>
    <errors/><warnings/>
    <time_taken_ms>8</time_taken_ms>
    <data num_results="1">
    <TAF><raw_text>TAF LFAT 220800Z 2209/2218 VRB02KT 9999 SCT020 PROB30 2209/2211 BKN010
    </raw_text>
                 <station_id>LFAT</station_id>
                 <issue_time>2019-10-22T08:00:00Z</issue_time>
                 <bulletin_time>2019-10-22T08:00:00Z</bulletin_time>
                 <valid_time_from>2019-10-22T09:00:00Z</valid_time_from>
                 <valid_time_to>2019-10-22T18:00:00Z</valid_time_to>
                 <latitude>50.52</latitude>
                 <longitude>1.62</longitude>
                 <elevation_m>14.0</elevation_m>
                 <forecast>
                                  <fcst_time_from>2019-10-22T09:00:00Z</fcst_time_from>
                                  <fcst_time_to>2019-10-22T18:00:00Z</fcst_time_to>
                                  <wind_dir_degrees>0</wind_dir_degrees>
                                  <wind_speed_kt>2</wind_speed_kt>
                                  <visibility_statute_mi>6.21</visibility_statute_mi>
                                  <sky_condition sky_cover="SCT" cloud_base_ft_agl="2000"/>
    </forecast>
    TAF>
    </data>
    </response>
    

    with QML code below:

        XmlListModel {
            id: xmlModel
            source:""
            query: "/response/data/TAF"
            XmlRole { name: "station_id"; query: "station_id/string()" }
            XmlRole { name: "fcst_time_from"; query: "forecast[1]/fcst_time_from/string()" }
            XmlRole { name: "fcst_time_to"; query: "forecast[1]/fcst_time_to/string()" }
            XmlRole { name: "wind_dir_degrees"; query: "forecast[1]/wind_dir_degrees/string()" }
            XmlRole { name: "wind_speed_kt"; query: "forecast[1]/wind_speed_kt/string()" }
            XmlRole { name: "visibility_statute_mi"; query: "forecast[1]/visibility_statute_mi/string()" }
            XmlRole { name: "sky_condition"; query: "forecast[1]/sky_condition/string()"  }
        }
    

    I read correctly all data except "sky_condition" because the beacon is not the same as other.

    How should I decode <sky_condition sky_cover="SCT" cloud_base_ft_agl="2000"/> compare <visibility_statute_mi>6.21</visibility_statute_mi> ?

    thank you very much for your help

    F 1 Reply Last reply
    0
    • F filipdns

      Hello,

      I need to parse XML like:

      <response version="1.2" xsi:noNamespaceSchemaLocation="http://aviationweather.gov/adds/schema/taf1_2.xsd">
      <request_index>19865992</request_index>
      <data_source name="tafs"/>
      <request type="retrieve"/>
      <errors/><warnings/>
      <time_taken_ms>8</time_taken_ms>
      <data num_results="1">
      <TAF><raw_text>TAF LFAT 220800Z 2209/2218 VRB02KT 9999 SCT020 PROB30 2209/2211 BKN010
      </raw_text>
                   <station_id>LFAT</station_id>
                   <issue_time>2019-10-22T08:00:00Z</issue_time>
                   <bulletin_time>2019-10-22T08:00:00Z</bulletin_time>
                   <valid_time_from>2019-10-22T09:00:00Z</valid_time_from>
                   <valid_time_to>2019-10-22T18:00:00Z</valid_time_to>
                   <latitude>50.52</latitude>
                   <longitude>1.62</longitude>
                   <elevation_m>14.0</elevation_m>
                   <forecast>
                                    <fcst_time_from>2019-10-22T09:00:00Z</fcst_time_from>
                                    <fcst_time_to>2019-10-22T18:00:00Z</fcst_time_to>
                                    <wind_dir_degrees>0</wind_dir_degrees>
                                    <wind_speed_kt>2</wind_speed_kt>
                                    <visibility_statute_mi>6.21</visibility_statute_mi>
                                    <sky_condition sky_cover="SCT" cloud_base_ft_agl="2000"/>
      </forecast>
      TAF>
      </data>
      </response>
      

      with QML code below:

          XmlListModel {
              id: xmlModel
              source:""
              query: "/response/data/TAF"
              XmlRole { name: "station_id"; query: "station_id/string()" }
              XmlRole { name: "fcst_time_from"; query: "forecast[1]/fcst_time_from/string()" }
              XmlRole { name: "fcst_time_to"; query: "forecast[1]/fcst_time_to/string()" }
              XmlRole { name: "wind_dir_degrees"; query: "forecast[1]/wind_dir_degrees/string()" }
              XmlRole { name: "wind_speed_kt"; query: "forecast[1]/wind_speed_kt/string()" }
              XmlRole { name: "visibility_statute_mi"; query: "forecast[1]/visibility_statute_mi/string()" }
              XmlRole { name: "sky_condition"; query: "forecast[1]/sky_condition/string()"  }
          }
      

      I read correctly all data except "sky_condition" because the beacon is not the same as other.

      How should I decode <sky_condition sky_cover="SCT" cloud_base_ft_agl="2000"/> compare <visibility_statute_mi>6.21</visibility_statute_mi> ?

      thank you very much for your help

      F Offline
      F Offline
      filipdns
      wrote on last edited by
      #2

      @filipdns I found solution myself:

      XmlRole { name: "sky_condition"; query: "forecast[1]/sky_condition/@sky_cover/string()" }

      thank you very much ;-)

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved