XmlListModel problem



  • Hi everyone,

    for this xml file

    @ <?xml version="1.0" encoding="iso-8859-1" ?>
    <catalogue>
    <book type="Hardcover">
    <title>C++ GUI Programming with Qt 4</title>
    <year>2006</year>
    <author>Jasmin Blanchette</author>
    <author>Mark Summerfield</author>
    </book>
    <book type="Paperback">
    <title>Programming with Qt</title>
    <year>2002</year>
    <author>Matthias Kalle Dalheimer</author>
    </book>
    </catalogue>@

    we can use this:
    XmlRole { name: "first_author"; query: "author[1]/string()" }

    anchor[1],anchor[2]......to get all auchors. But what can we do if we dont know how many authors in the list?that is to say, how can we know the end of the auchors

    Thank you very much in advance

    [EDIT: fixed square bracket markup, Volker]



  • You might make an XmlListModel based on a query that resolves to the specific book you are viewing, and that lists the authors in that part of the XML file. I use something like this myself, and it works well.



  • Hi,

    For reference, "QTBUG-13688":http://bugreports.qt.nokia.com/browse/QTBUG-13688 is the current suggestion tracking this issue, and the first comment points to some possible workarounds. If you have any suggestions for how we better handle this in the future, comments on the bug report would be most welcome.

    Regards,
    Michael



  • Hi,

    Thank you for the reply

    But i cant understant the first comment.
    @XmlListModel {
    source: "some.xml"
    query: "items/item"

    onCountChanged: {
    console.log( JSON.stringify(items));

    for(var itemId in items) {
       for(var thingId in items[itemId]) {
          console.log("Item", itemId, "::, JSON.stringify( items[itemId][thingId]));  
       }
    }
    

    }
    }@

    Why are there "Json"s ?
    and in my opinion, each xmlRole is like the listelement for the list model.
    how can i define xmlroles for this solution?

    [quote author="mbrasser" date="1299631847"]Hi,

    For reference, "QTBUG-13688":http://bugreports.qt.nokia.com/browse/QTBUG-13688 is the current suggestion tracking this issue, and the first comment points to some possible workarounds. If you have any suggestions for how we better handle this in the future, comments on the bug report would be most welcome.

    Regards,
    Michael[/quote]



  • Hi Andre,

    Thank you for the reply.

    But how can i do if i do know "specific book ", that is to say, in this example, there are no "type" for "book".

    futhermore, i dont understand clearly your solution. the problem is that: i dont know how many authors for a book, i cant write a loop to iterate all anthors.

    [quote author="Andre" date="1299606028"]You might make an XmlListModel based on a query that resolves to the specific book you are viewing, and that lists the authors in that part of the XML file. I use something like this myself, and it works well.[/quote]



  • I am sorry my comment wasn't that clear. Let me try to fix that, and explain better how I work around this limitation.

    I am essentially using multiple XmlListModels. The first model queries the books. When displaying the data for a book in a delegate (or on a different page where you go to when clicking your delegate or something like that) you can create a new XmlListModel (local to that delegate), this time with a query that only lists the authors for that specific book your delegate is displaying. Your XML needs to make it possible to uniquely identify a specific book though, so you can make an XML role for that bit of data in your main XmlListModel, and use it to construct the query for the author XmlListModel.

    Edit: Note that you can know the number of authors. You can define a role for that in your main "books" XmlListModel, as xpath allows queries for counting items too.



  • Thank you very much Andre,

    I get what you wanted to say. Let me symplify my problem. In the following code:
    @<catalogue>
    <book>
    <author>a1</author>
    <author>a2</author>
    <author>a3</author>
    <author>a4</author>
    <author>a5</author>
    <author>a6</author>
    ......
    </book>
    </catalogue>@
    As you can see, it's not the problem of different levels' model. But it is:

    If i dont know how many authors for a book. i cant write

    @XmlRole{name:"author1";query:"auchor[1]/string"}
    XmlRole{name:"author2";query:"auchor[2]/string"}
    XmlRole{name:"author3";query:"auchor[3]/string"}
    ...@

    I asked this question because i have different XML files, where tags are almost the same but auchor's number varies.

    Do you have some suggestions for this?

    Thanks in advance
    [quote author="Andre" date="1299666468"]I am sorry my comment wasn't that clear. Let me try to fix that, and explain better how I work around this limitation.

    I am essentially using multiple XmlListModels. The first model queries the books. When displaying the data for a book in a delegate (or on a different page where you go to when clicking your delegate or something like that) you can create a new XmlListModel (local to that delegate), this time with a query that only lists the authors for that specific book your delegate is displaying. Your XML needs to make it possible to uniquely identify a specific book though, so you can make an XML role for that bit of data in your main XmlListModel, and use it to construct the query for the author XmlListModel.

    Edit: Note that you can know the number of authors. You can define a role for that in your main "books" XmlListModel, as xpath allows queries for counting items too.[/quote]



  • hulyyige, I don't think your approach is possible, sorry. You can count the number of authors for a book, but you can not dynamically create roles for each author individually. Not that I know of, anyway. That is I why I suggested a two-step approach, where you create a new XmlListModel just for the authors of your specific book. But, it would require an XML structure like this, where each book as a unique identifier that you can use in an XPath query:

    @<catalogue>
    <book id="1">
    <author>a1</author>
    <author>a2</author>
    <author>a3</author>
    <author>a4</author>
    <author>a5</author>
    <author>a6</author>
    ......
    </book>
    </catalogue>@

    What might be possible, is to concatenate the different authors into a single role with a clever XPath query. I am not very experienced in that, so I can not help you with that, but that may be simpler way out. No idea if Qt's XPath implementation supports that though, or if it possible at all.



  • [quote author="huluyige" date="1299665774"]Thank you for the reply

    But i cant understant the first comment.[/quote]

    Hi,

    Apologies for the confusion. What I meant to point out is that the first comment has a link to the mailing list, where there was an earlier discussion on this topic, including discussion about possible workarounds.

    Regards,
    Michael


Log in to reply
 

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