Results 1 to 4 of 4

Thread: can QXmlQuery query on a QString which holds the whole XML document

  1. #1
    Join Date
    Jun 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question can QXmlQuery query on a QString which holds the whole XML document

    Looks like QXmlQuery only queries an XML file on disk. My situation is all my XML docs are saved in a QStringList, each QString is a XML doc. I don't want to save all the XML docs into files for QXmlQuery to do the query. Is there anyway for QXmlQuery to query on the XML doc in my QString?

  2. #2
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: can QXmlQuery query on a QString which holds the whole XML document

    maybe QBuffer can help you...

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: can QXmlQuery query on a QString which holds the whole XML document


  4. #4
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: can QXmlQuery query on a QString which holds the whole XML document

    Is this the correct way of doing this?

    Qt Code:
    1. QFile file("./books.xml");
    2. doc.setContent(&file, true);
    3.  
    4. QByteArray xmlDoc = doc.toByteArray();
    5. QBuffer xmlBuffer(&xmlDoc);
    6. xmlBuffer.open(QIODevice::ReadOnly);
    7. QXmlQuery query;
    8. query.bindVariable("xmlDoc", &xmlBuffer);
    9. query.setQuery("doc($xmlDoc)/bookstore/book[price>30]/title");
    10.  
    11. QString testOutput;
    12. query.evaluateTo(&testOutput);
    13. cout << testOutput.toStdString() << endl;
    To copy to clipboard, switch view to plain text mode 

    Given the contents of books.xml (shown at bottom of this post), I get the following results (which appear correct).
    Qt Code:
    1. <title lang="en">XQuery Kick Start</title>
    2. <title lang="en">Learning XML</title>
    To copy to clipboard, switch view to plain text mode 

    books.xml contents:
    Qt Code:
    1. <?xml version="1.0" encoding="ISO-8859-1"?>
    2.  
    3. <bookstore>
    4.  
    5. <book category="COOKING">
    6. <title lang="en">Everyday Italian</title>
    7. <author>Giada De Laurentiis</author>
    8. <year>2005</year>
    9. <price>30.00</price>
    10. </book>
    11.  
    12. <book category="CHILDREN">
    13. <title lang="en">Harry Potter</title>
    14. <author>J K. Rowling</author>
    15. <year>2005</year>
    16. <price>29.99</price>
    17. </book>
    18.  
    19. <book category="WEB">
    20. <title lang="en">XQuery Kick Start</title>
    21. <author>James McGovern</author>
    22. <author>Per Bothner</author>
    23. <author>Kurt Cagle</author>
    24. <author>James Linn</author>
    25. <author>Vaidyanathan Nagarajan</author>
    26. <year>2003</year>
    27. <price>49.99</price>
    28. </book>
    29.  
    30. <book category="WEB">
    31. <title lang="en">Learning XML</title>
    32. <author>Erik T. Ray</author>
    33. <year>2003</year>
    34. <price>39.95</price>
    35. </book>
    36.  
    37. </bookstore>
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to brcain for this useful post:

    azalea (31st May 2018)

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. Possible to execute QXmlQuery on a QString?
    By chadkeck in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2008, 17:24
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.