Results 1 to 1 of 1

Thread: Extracting attributes value from an XML file with QXmlQuery and XPath

  1. #1
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Extracting attributes value from an XML file with QXmlQuery and XPath

    Hey !

    I am trying to read attributes value in a XML file.

    Take this file "data.xml" :

    Qt Code:
    1. <?xml version="1.0" encoding="ISO-8859-1" ?>
    2. <a name="na">
    3. </a>
    To copy to clipboard, switch view to plain text mode 

    I want to get as result of a query the string "na".

    I think I found a proper solution which follows :

    Qt Code:
    1. // Open a file
    2. QString filename(
    3. "C:\\Work\\data.xml"
    4. );
    5. QFile file(filename);
    6. file.open(QIODevice::ReadOnly);
    7.  
    8. // Query
    9. QXmlQuery query;
    10. query.bindVariable("fileName", &file);
    11. query.setQuery("declare variable $fileName external; doc($fileName)/a/@name");
    12. // or
    13. // query.setQuery("declare variable $fileName external; doc($fileName)/a/@name/data(.)");
    14.  
    15. // Result
    16. QXmlResultItems xmlResultItems;
    17. query.evaluateTo(&xmlResultItems);
    18.  
    19. // Try to get the first item
    20. QXmlItem item(xmlResultItems.next());
    21. while(!item.isNull())
    22. {
    23. if(item.isAtomicValue())
    24. {
    25. // Output for the query "/a/@name/data(.)"
    26. qDebug() << "Atomic value : " << item.toAtomicValue().toString();
    27. }
    28. else if(item.isNode())
    29. {
    30. // Output for the query "/a/@name"
    31. qDebug() << "Node : " << item.toNodeModelIndex().stringValue();
    32. }
    33. else
    34. {
    35. // Item must be null
    36. }
    37.  
    38. // Next item
    39. item = xmlResultItems.next();
    40. }
    41.  
    42. // Close the file
    43. file.close();
    To copy to clipboard, switch view to plain text mode 

    Am I right ?

    Initialy, I had the result in a QString instead of a QXmlResultItems and I was unable to retrieve the attribute value with the query "/a/@name" (but it worked with "/a/@name/data(.)"). I am not sure why and I still don't know how to make it works with :

    Qt Code:
    1. // Result
    2. QString string;
    3. query.evaluateTo(&string);
    4.  
    5. // Output : write nothing
    6. qDebug() << string;
    To copy to clipboard, switch view to plain text mode 

    Thanks.
    Last edited by moijhd; 5th July 2013 at 17:33.

Similar Threads

  1. problem with using xpath to query from xml file
    By aya_lawliet in forum Qt Quick
    Replies: 0
    Last Post: 7th September 2011, 15:38
  2. Xpath & QT
    By pl01 in forum Qt Programming
    Replies: 0
    Last Post: 15th November 2010, 12:48
  3. Replies: 7
    Last Post: 14th June 2010, 02:42
  4. How to change the attributes of a file
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2010, 17:26
  5. Updating File attributes and it's summary details
    By senthilsp in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2009, 13:31

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.