Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Read XML that contains multiple elements with same name using XmlListModel
Forum Updated to NodeBB v4.3 + New Features

Read XML that contains multiple elements with same name using XmlListModel

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rush
    wrote on 12 Mar 2014, 14:28 last edited by
    #1

    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()" }@

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bidochon
      wrote on 12 Mar 2014, 14:58 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rush
        wrote on 13 Mar 2014, 07:03 last edited by
        #3

        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>@

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bidochon
          wrote on 13 Mar 2014, 12:46 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rush
            wrote on 13 Mar 2014, 13:21 last edited by
            #5

            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 :)

            1 Reply Last reply
            0

            1/5

            12 Mar 2014, 14:28

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved