Important: Please read the Qt Code of Conduct -

[Solved]XmlListModel problem

  • Hi All,
    I have a problem when I trying get the data from xml.
    My question is how can I get 'Shanghai' and 'Beijing' at the same time,
    My code as below: @
    import QtQuick 1.0

    XmlListModel {
    id: cityModel

    source: "city.xml"  
    query: "/cities/city"    // ======> when I try to use '/cities/[A-B]/city' it is doesn't work.
    XmlRole { name: "name"; query: "name/string()" }
    XmlRole { name: "id";   query: "id/string()"   }


    <?xml version="1.0" encoding="UTF-8"?>

    Edit : please add @ tags,
    tags added; Eddy

  • It's hard to understand this without formatting. Can you enclose the code in code tags so we can see better?

    I'm going to need to see the XML in its correct format.

  • Hi,

    You should be able to get all cities using

    @query: "/cities//city"@


    @query: "/cities/*/city"@

    If you only wanted e.g. the cities under A and B but not those under C, you could also try something like

    @query: "/cities/*[self::A or self::B]/city"@

    (in theory I think "/cities/A/city | /cities/B/city" should also work, but XmlListModel doesn't currently support this)


  • Hi Michael,
    I used "/cities//city", now It works! Thank you very much.
    Can you give some information about the answer, If I can't find more information about 'query:' in Qt Assistant where can I find it?

  • "Query" uses the Xpath syntax. You can find some info here:

Log in to reply