Solved xml parsing
-
hi any body
i'm new in qt quick
i have a problem in pars xmlmy xml file is
//*************************************************
<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album>
<title>Air Show</title>
<img src="http://xmlgrid.net/demo/pic/rsz_a1.jpg" />
</album>
<album>
<title>Cruise</title>
<img src="http://xmlgrid.net/demo/pic/rsz_c1.jpg" />
</album>
</gallery>//************************************************** **
how can fetch image source from <img src ... > ?
XmlListModel {
id: imageModel
//source: "http://xmlgrid.net/demo/sample1.xml"
source: "/asset/sample1.xml"
query: "/gallery/album"
XmlRole { name: "imageSource"; query: "img/@src/string()" }
XmlRole { name: "title"; query: "title/string()" }
} -
@bahman309 Use @ to get the attributes.
Example:
import QtQuick 2.5 import QtQuick.Controls 1.4 import QtQuick.XmlListModel 2.0 ApplicationWindow { visible: true width: 640 height: 480 XmlListModel { id: xmlModel source: "/res/myxml.xml" query: "/gallery/album" XmlRole { name: "title"; query: "title/string()" } // query the img's src (the '@' indicates 'src' is an attribute, not an element) XmlRole { name: "img"; query: "img/@src/string()" } } ListView { width: 180; height: 300 model: xmlModel delegate: Text { text: title + ": " + img } } }
-
thank you for your reply
I use @ but not work -
@bahman309 Are you getting the
title
or is it also not working?I've uploaded my code to GitHub. Use it to check where the error is.
-
@tarod.net
it is so strange
when i use your code in my project, I get the title but for image just get "undefined",
and when i use your code in a new project, every thing is Ok ...thank you so much
you are so kind and so expert -
@bahman309 And what about your code in a new project?
So, did you solve the problem? I could see more code if you want to or even the whole project. Just tell me where to download it.
-
when i switch between the projects and then run my projects, it's work !!!
-
@bahman309 Well, great then! :) Happy coding! Please, mark the post as solved.
-
thank you so much