Results 1 to 8 of 8

Thread: XML: accessing deep nodes

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default XML: accessing deep nodes

    Hello. Is there a way to access XML nodes with something like this:
    Qt Code:
    1. domDoc.elementsByTagName("methodResponse/params/param/value/struct/member")
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. QDomElement elem = domDoc.firstChildElement("methodResponse").firstChildElement("params").firstChildElement("param").firstChildElement("value").firstChildElement("struct").firstChildElement("member")
    To copy to clipboard, switch view to plain text mode 

    Here follows the sample XML document. I need to get access to all 'member' nodes.
    Qt Code:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <methodResponse>
    3. <params>
    4. <param>
    5. <value>
    6. <struct>
    7. <member>
    8. <name>token</name>
    9. <value>
    10. <string>532236bdf362493h5</string>
    11. </value>
    12. </member>
    13. <member>
    14. <name>status</name>
    15. <value>
    16. <string>200 OK</string>
    17. </value>
    18. </member>
    19. <member>
    20. <name>seconds</name>
    21. <value>
    22. <double>0.008</double>
    23. </value>
    24. </member>
    25. </struct>
    26. </value>
    27. </param>
    28. </params>
    29. </methodResponse>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML: accessing deep nodes

    See QXmlQuery.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Location
    Munich
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: XML: accessing deep nodes

    Or You can use XPath to do the Query

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: XML: accessing deep nodes

    Ok, thanks. Some another question... I receive xml from the network. It seems like QXmlQuery allows to use QNetworkReply in bindVariable method, e.g.
    Qt Code:
    1. query.bindVariable("MyXml", reply);
    To copy to clipboard, switch view to plain text mode 
    But it doesn't work: evaluateTo hangs up for 5-10 seconds, then I see "First-chance exception at 0x760eb727 in SnelNL.exe: Microsoft C++ exception: bool at memory location 0x0034c20f.." in the output window.

    However if I read data to QByteArray and create QBuffer with this data, query works fine.

    Is it possible to use QNetworkReply directly in QXmlQuery::bindVariable as XML source?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML: accessing deep nodes

    Why not just use the doc() directive in the query itself?

    Qt Code:
    1. QXmlQuery query;
    2. query.setQuery("doc(http://www.qtcentre.org/index.php)/html/body/whatever");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: XML: accessing deep nodes

    I need to make a post request in order to receive a reply from server

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML: accessing deep nodes

    In that case use QXmlQuery::setFocus() and pass it the network reply you receive. Just make sure you do it only after the data you expect has arrived.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: XML: accessing deep nodes

    Thanks, but still the same problem when I pass a reply as QIODevice. Below is the slot that is connected to QNetworkAccessManager::finished signal, so all the data must be arrived, I think.
    Qt Code:
    1. void MyClass::authenticationFinished(QNetworkReply *reply)
    2. {
    3. QXmlQuery query;
    4. query.setFocus(reply);
    5. query.setQuery("methodResponse/params/param/value/struct/member[name=\"token\"]/value/string(string)");
    6. // ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    The following code works fine:
    Qt Code:
    1. void MyClass::authenticationFinished(QNetworkReply *reply)
    2. {
    3. QXmlQuery query;
    4. query.setFocus(reply->readAll()); // !pass the string
    5. query.setQuery("methodResponse/params/param/value/struct/member[name=\"token\"]/value/string(string)");
    6. // ...
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Deep copy of widget hierarchy
    By ajoffe in forum Qt Programming
    Replies: 2
    Last Post: 14th December 2009, 06:36
  2. QFtp: entering deep folder (cd)
    By Raccoon29 in forum Newbie
    Replies: 1
    Last Post: 13th May 2008, 09:28
  3. QVector, containers, deep copy
    By TheKedge in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 05:45
  4. Do assignment operators in Qt4 return deep or shallow copy?
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 09:01
  5. [QT4] QTreeView and expandable nodes
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2006, 16:52

Tags for this Thread

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.