Read XML that contains multiple elements with same name using XmlListModel



  • Hi all,

    I'm trying to read an XML file that looks like this:

    ......
    <category>
    some data
    </category>
    <category>
    some other data
    </category>
    <category>
    again different data
    </category>
    ......

    I'm using QML XmlListModel with XmlRole to grab data from the XML file. How can I make it read multiple elements with the same name? Is there some option to make it combine all data into one variable, or something like that?

    If I try it just as it is, I get the following error:
    Error XPTY0004 in file:///home/denisd/qt_projects/application, at line 2, column 57: Required cardinality is zero or one("?"); got cardinality one or more("+").

    XmlRole code I use is:
    @XmlRole { name: "category"; query: "category/string()" }@



  • Stupid question but is there a top name element such as
    @<my_list>
    <category>some data</category>
    <category>some other data</category>
    ….
    </my_list>@

    if not, I don't think it's a valid XML format.



  • Jep, there is a top level element, this was just a part of the XML that I'm trying to read. The XML is actually an RSS feed:

    @<rss>
    <chanel>
    <item>
    .
    .
    <category>
    some data
    </category>
    <category>
    some other data
    </category>
    </item>
    </channel>
    </rss>@



  • I found this in the tableView tutorial example yesterday. Maybe that can help you

    @
    XmlListModel {
    id: flickerModel
    source: "https://api.flickr.com/services/feeds/photos_public.gne?format=rss2&tags=" + "Qt"
    query: "/rss/channel/item"
    namespaceDeclarations: "declare namespace media="http://search.yahoo.com/mrss/";"
    XmlRole { name: "title"; query: "title/string()" }
    XmlRole { name: "imagesource"; query: "media:thumbnail/@url/string()" }
    XmlRole { name: "credit"; query: "media:credit/string()" }
    }
    @

    I never used Xmlrole myself.



  • Here for future reference, I managed to solve it:

    @ query: "/rss/channel/item"
    XmlRole { name: "category"; query: "string-join(category, ',')" }@

    This joins all <category> elements separated by ','.

    Bldochon, tnx for the help :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.