Results 1 to 2 of 2

Thread: QXmlQuery query results

  1. #1
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default QXmlQuery query results

    I'm having a really annoying time with this.

    I figured I'd use XML queries rather than manually traversing with XML DOM, because it would make my life easier. Well, so far that's not the case.

    I have a sample XML file, as follows:

    Qt Code:
    1. <root>
    2. <mynode id="1">
    3. <foo>this is foo.</foo>
    4. <bar>this is bar.</bar>
    5. </mynode>
    6. <mynode id="2">
    7. <foo>this is foo.</foo>
    8. <bar>this is bar.</bar>
    9. </mynode>
    10. <mynode id="3">
    11. <foo>this is foo.</foo>
    12. <bar>this is bar.</bar>
    13. </mynode>
    14. <mynode id="4">
    15. <foo>this is foo.</foo>
    16. <bar>this is bar.</bar>
    17. </mynode>
    18. </root>
    To copy to clipboard, switch view to plain text mode 

    I want to find all nodes called "mynode".
    At first I looked into evaluateTo(QXmlResultItems * result) but this doesn't let me access any properties of the nodes themselves, because QXmlItemis a very simplistic class.

    Next I looked at evaluateTo(QStringList * target) but this returns the text of all descendants of "mynode" nodes as well, so I get all the "this is foo" and "this is bar". I don't want this.

    What I want is the ability to match a bunch of nodes, and then access/manipulate each one individually. Is this possible?

    [edit]
    I found this example here: http://stackoverflow.com/questions/1...-queries-in-qt

    But when I run it, elementsByTagName only returns a single mynode, the first one. I don't understand what I'm doing wrong, since "res" contains the whole tree.
    Last edited by JovianGhost; 1st September 2010 at 02:44.

  2. #2
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QXmlQuery query results

    Ok, I found the problem. The example in the link is incorrect.
    When using setContent, the results need to be enclosed in another, arbitrary (root) tag. So it should read:

    Qt Code:
    1. rhythmdb.setContent("<sometag>" + res + "</sometag>");
    To copy to clipboard, switch view to plain text mode 

    Now it works, but this seems like a really kludgy way of doing things-- executing the query, dumping the results into an XML buffer, than traversing the buffer.
    I've always liked Qt because it's extremely powerful and easy to use at the same time. But this is just terrible. There MUST be an easier way than this.

Similar Threads

  1. Problem with QXmlQuery
    By antialias in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2010, 14:16
  2. Replies: 3
    Last Post: 1st October 2009, 23:19
  3. Help needed with QXmlQuery
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 12:25
  4. Can't get QXmlQuery to return results
    By bpetty in forum Newbie
    Replies: 2
    Last Post: 13th May 2008, 14:27
  5. QTreeView with results from an SQL query.
    By ederbs in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 02:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.