PDA

View Full Version : Receiving XML document from server and parsing it into strings



Plox
28th August 2013, 19:33
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



Rectangle {
width: 350; height: 400

function showRequestInfo(text) {
log.text = log.text + "\n" + text
console.log(text)
}

Text { id: log; anchors.fill: parent; anchors.margins: 10 }

Rectangle {
id: button
anchors.horizontalCenter: parent.horizontalCenter; anchors.bottom: parent.bottom; anchors.margins: 10
width: buttonText.width + 10; height: buttonText.height + 10
border.width: mouseArea.pressed ? 2 : 1
radius : 5; smooth: true

Text { id: buttonText; anchors.centerIn: parent; text: "Request" }

MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: {
log.text = ""
console.log("\n")

var doc = new XMLHttpRequest();
doc.onreadystatechange = function() {
if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {


} else if (doc.readyState == XMLHttpRequest.DONE) {
var a = doc.responseXML.documentElement;
for (var ii = 0; ii < a.childNodes.length; ++ii) {
showRequestInfo(a.childNodes[ii].nodeName);
}
}
}


doc.open("POST", 'http://www.example.com/mydata.xml');
doc.send();
}
}
}
}
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:



is-admin
#text
signature
#text
restrictions
#text

How can I fix this and manage to save the information retrieved with the xml document into strings?

Thanks a LOT in advance!

wysota
28th August 2013, 22:44
What help exactly do you expect from us? Your problem seems solely related to JavaScript.

ChrisW67
29th August 2013, 00:48
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().

wysota
29th August 2013, 07:51
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.

Plox
29th August 2013, 10:34
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

anda_skoa
3rd September 2013, 10:35
Have you tried accessing anything else of a.childNodes[ii]?
Maybe it has a.childNodes[ii].text?

Cheers,
_