[Solved] XmlsListModel.get() won't work.
-
... and my obvious question is: what I have missed?
I have xml feeds that should be managed as data. Due to a workaround of the "QT-BUG 12953":https://bugreports.qt.nokia.com/browse/QTBUG-12953 I am trying to provide a definitely way to manage them using QML and Js. I have tried to manage one of my xml files with not so good results. So, I have created a silly xml file and the results are the same as I explain below:
The xml file is the following test.xml (just copied from the XmlListModel documentation):
@
<?xml version="1.0" encoding="utf-8"?>
<test version="2.0">
<channel>
<item>
<title>A blog post</title>
</item>
<item>
<title>Another blog post</title>
</item>
</channel>
</test>
@
At this point I create the XmlListModel as follows:
@
XmlListModel{
id: quicklinksXML
source: "../data/download/config/test.xml"
query: "/test/channel/item"XmlRole { name: "title"; query: "title/string()" } }
@
Then, from a js I try to get a value from the list model:
@
var test = quicklinksXML.get(0).title;
console.log("<<<XML>>> = " + test)
@
(always following the example of the documentation)At this point the question is: why I have always a [Undefined] result?
The second but not less important question is: having a xml field in the format
@
...
<field parameter="value">
Data of the fiels
</field>
...
@
Do you know if there is a way to read the parameter="value" content, that is not a field but part of the xml element?Thank to all.
-
What about the status code of your model ?
@var sc = quicklinksXML.status; conslole.log(sc + " error code " + quicklinksXML.errorString)@
About the parameter something like the code below should work,
@XmlRole { name: "field"; query: "field/@parameter" }@
-
Mmm... The error code is managed by a case, because I will see what are the steps: loading, the data ready, that's when I try to read. The role as I wrote above is as I saw in the example. Why do you suggest the field/@parameter ? As I know it should be the content type of the field...
-
Yes exctly. Sorry if I was unclear.
I am interested to this question from the QML document side, I saw that if the problem can't be solved I will manage a class in C++ but from QML / Js is best.
Thank you again.
-
The problem was solved with some tricks and some workarounds: using the get() it is not sufficient when the XmlListModel.status is Ready. As a matter of fact, it is possible that in the exact instant when you call the get() the Ready condition is true but the data for some reason are not yet available to the QML. Thus it should be added a control if the get() value is Undefined (=null) or not.
Excluding this, all the suggestions of @geronik was correct. The best practice is managing a onStatusChange event checking the Ready condition in conjunction with a XmlListModel.get(0).parameter control for != nullI am preparing a small component based on the XmlListModel to simplify the management of the xml files when these should be treated in a different way than lists.