XML Cannot read property 'title' of undefined
-
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 -
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.
-
@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 -
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 }