Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. XML Cannot read property 'title' of undefined
Forum Updated to NodeBB v4.3 + New Features

XML Cannot read property 'title' of undefined

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.7k Views 1 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.
  • T Offline
    T Offline
    t333o
    wrote on 21 Dec 2015, 16:04 last edited by
    #1

    Hello,
    i am trying to read a xml doc, but i allways get "TypeError: Cannot read property 'title' of undefined"

    The XML doc look like this:

    <?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0">
    
    <channel>
    
     <item>
       <title>Spot1</title>
     </item>
     <item>
       <title>Spot2</title>
     </item>
    </channel>
    
    </rss>
    

    this the relevant part of my qml file:

     XmlListModel {
            id: spotlightxml
            source: "http://example.de/spotlight.xml"
            query: "/rss/channel/item"
    
            XmlRole { name: "title"; query: "title/string()" }
        }
    Text {
            id: text2
            x: 7
            y: 39
            text: spotlightxml.get(1).title
            font.pixelSize: 12
        }
    
    

    What can I do to fix it? Thanks in advance :)
    t333o

    1 Reply Last reply
    0
    • V Offline
      V Offline
      ValentinMichelet
      wrote on 21 Dec 2015, 21:48 last edited by
      #2

      When I visit your link (http://example.de/spotlight.xml) it's empty, maybe the error comes from this. Otherwise you pretty copied the example from the documentation, and nothing seems wrong, for what I've seen.

      T 1 Reply Last reply 22 Dec 2015, 10:39
      1
      • V ValentinMichelet
        21 Dec 2015, 21:48

        When I visit your link (http://example.de/spotlight.xml) it's empty, maybe the error comes from this. Otherwise you pretty copied the example from the documentation, and nothing seems wrong, for what I've seen.

        T Offline
        T Offline
        t333o
        wrote on 22 Dec 2015, 10:39 last edited by
        #3

        @ValentinMichelet Thanks for your answer;)
        I changed the XML Source only for the forum to example.com/spotlight.xml. I also get xml.status.ready......so i really dont know. Should i use some namespaceDeclarations? -> i know nothing about it XD

        1 Reply Last reply
        0
        • V Offline
          V Offline
          ValentinMichelet
          wrote on 22 Dec 2015, 11:31 last edited by
          #4

          OK, I found it.

          You are trying to access data from a model. To do this, the best option is to use a view:

          XmlListModel {
            id: spotlightxml;
            source: "http://example.de/spotlight.xml";
            query: "/rss/channel/item";
          
            XmlRole {
              name: "title";
              query: "title/string()";
            }
          }
          
          ListView {
              width: 180;
              height: 300;
              model: spotlightxml;
              delegate: Text {
                text: title;
             }
          }
          

          The view is connected to the model and some internal mechanisms determine when data are ready to be read. If you really don't want to use a view, here is a trick I found on http://comments.gmane.org/gmane.comp.lib.qt.qml/510

          XmlListModel {
            id: spotlightxml;
            source: "http://example.de/spotlight.xml";
            query: "/rss/channel/item";
          
            XmlRole {
              name: "title";
              query: "title/string()";
            }
          
            onStatusChanged: {
              if (status == XmlListModel.Ready) {
                // Displays data when model is ready
                titleText.text = get(1).title;
              }
            }
          }
          
          Text {
            id: titleText
            x: 7
            y: 39
            font.pixelSize: 12
          }
          
          1 Reply Last reply
          1

          1/4

          21 Dec 2015, 16:04

          • Login

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