-
Notifications
You must be signed in to change notification settings - Fork 0
3.01
Explanation of the contents of a topic page @ Topic reference page
Objective: OBJECTIVE
J:While XML is something to be introduced, at least for the starters we probably won't need exercises for it.
What is XML?
How do I add XML data to Quick models?
Qt Quick provides an easy and convenient way to construct models from XML data.
If we have XML file of the form
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
...
<channel>
<item>
<title>A blog post</title>
<pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
</item>
<item>
<title>Another blog post</title>
<pubDate>Sat, 07 Sep 2010 15:35:01 GMT</pubDate>
</item>
</channel>
</rss>
We can construct a model the following way:
import QtQuick 2.0
import QtQuick.XmlListModel 2.0
XmlListModel {
id: xmlModel
source: "http://www.mysite.com/feed.xml"
query: "/rss/channel/item"
XmlRole { name: "title"; query: "title/string()" }
XmlRole { name: "pubDate"; query: "pubDate/string()" }
}
In source
we define the location for the .xml file, which can be either local or remote address. The query
value of "/rss/channel/item" specifies that the XmlListModel should generate a model item for each in the XML document.
The XmlRole
objects define the model item attributes. Here, each model item will have title
and pubDate
attributes that match the title
and pubDate
values of its corresponding . (See XmlRole::query
for more examples of valid XPath expressions for XmlRole.)
Using the generated models in ListView
works as usual:
ListView {
width: 180; height: 300
model: xmlModel
delegate: Text { text: title + ": " + pubDate }
}
https://doc-snapshots.qt.io/qt5-5.9/qtquick-modelviewsdata-modelview.html#xml-model https://qmlbook.github.io/en/ch06/index.html#a-model-from-xml