Results 1 to 6 of 6

Thread: Receiving XML document from server and parsing it into strings

  1. #1
    Join Date
    Aug 2013
    Posts
    15
    Thanks
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Receiving XML document from server and parsing it into strings

    Good day fellow programmers,

    I am trying to communicate with a rest API web service, get a xml document and then parse it’s values into strings.
    Since I am new to this topic any help would be greatly appreciated!

    This is what I built using the XMLHTTPREQUEST example:
    import QtQuick 2.0

    Qt Code:
    1. Rectangle {
    2. width: 350; height: 400
    3.  
    4. function showRequestInfo(text) {
    5. log.text = log.text + "\n" + text
    6. console.log(text)
    7. }
    8.  
    9. Text { id: log; anchors.fill: parent; anchors.margins: 10 }
    10.  
    11. Rectangle {
    12. id: button
    13. anchors.horizontalCenter: parent.horizontalCenter; anchors.bottom: parent.bottom; anchors.margins: 10
    14. width: buttonText.width + 10; height: buttonText.height + 10
    15. border.width: mouseArea.pressed ? 2 : 1
    16. radius : 5; smooth: true
    17.  
    18. Text { id: buttonText; anchors.centerIn: parent; text: "Request" }
    19.  
    20. MouseArea {
    21. id: mouseArea
    22. anchors.fill: parent
    23. onClicked: {
    24. log.text = ""
    25. console.log("\n")
    26.  
    27. var doc = new XMLHttpRequest();
    28. doc.onreadystatechange = function() {
    29. if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
    30.  
    31.  
    32. } else if (doc.readyState == XMLHttpRequest.DONE) {
    33. var a = doc.responseXML.documentElement;
    34. for (var ii = 0; ii < a.childNodes.length; ++ii) {
    35. showRequestInfo(a.childNodes[ii].nodeName);
    36. }
    37. }
    38. }
    39.  
    40.  
    41. doc.open("POST", 'http://www.example.com/mydata.xml');
    42. doc.send();
    43. }
    44. }
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 
    This code does successfully communicate with the server and retrieves information from it.
    The problem is that it only names the node’s name but leaves the values out.

    Example response:

    Qt Code:
    1. is-admin
    2. #text
    3. signature
    4. #text
    5. restrictions
    6. #text
    To copy to clipboard, switch view to plain text mode 
    How can I fix this and manage to save the information retrieved with the xml document into strings?

    Thanks a LOT in advance!

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

    Default Re: Receiving XML document from server and parsing it into strings

    What help exactly do you expect from us? Your problem seems solely related to JavaScript.
    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,555
    Thanks
    13
    Thanked 1,581 Times in 1,510 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Receiving XML document from server and parsing it into strings

    That Javascript only seems to handle the nodeName (and nodeType?) and makes no attempt to handle the nodeValue... but then I am not a JS aficionado and we cannot see showRequestInfo().

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

    Default Re: Receiving XML document from server and parsing it into strings

    Quote Originally Posted by ChrisW67 View Post
    we cannot see showRequestInfo().
    It's in the beginning of the first snippet but it doesn't do much apart appending its argument to a variable. And the argument happens to be the node name thus I'm not suprised that the tag content is not processed anywhere.
    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.


  5. #5
    Join Date
    Aug 2013
    Posts
    15
    Thanks
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Receiving XML document from server and parsing it into strings

    Dear wysota and ChrisW67,
    Thanks a lot for your fast answers.

    I posted this question in this forum since I wasn't sure whether I shouldn't e.g use QNetworkAccessManager or similar neat qt features.

    Would you be so kind to help me fix that problem with showRequestInfo() ?
    But besides that I am aware that this code can not handle any values since I don't know how to implement that yet,

    Thanks!

    Kind regards,
    Plox

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Receiving XML document from server and parsing it into strings

    Have you tried accessing anything else of a.childNodes[ii]?
    Maybe it has a.childNodes[ii].text?

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 13th July 2014, 04:09
  2. Create fragments of XML while parsing XML document
    By bleze in forum Qt Programming
    Replies: 2
    Last Post: 3rd January 2013, 11:21
  3. Replies: 8
    Last Post: 18th December 2010, 16:07
  4. QRegExp parsing matched quoted strings
    By jhndnn in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2010, 00:05

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.