I am trying to learn how to parse an XML string, and have been following the documentation, but am getting an error. The following is my basic code:
xfer = CreateObject("roURLTransfer")
xfer.SetURL("http://rokudev.roku.com/rokudev/examples/register/getRegCode")
webService = xfer.GetToString()
xml=CreateObject("roXMLElement")
If xml.Parse(webService) then
print(xml.result.status[0])
End If
According to page 17 of the BrightScript 3.0 reference guide, the line xml.result.status[0] should print out “success”, but I’m getting an error “Type Mismatch”
“brocker” wrote:
I changed the code to be the following:
If xml.Parse(webService) then
Print(xml.status.GetText())
End If
And I’m still getting “Type Mismatch”. Does the code look like what you were meaning?
That looks right to me. The problem may be in another part of your code. The console will tell you which line of code is causing the error.
This works correctly and prints “success” to the console…
xfer = CreateObject("roURLTransfer")
xfer.SetURL("http://rokudev.roku.com/rokudev/examples/register/getRegCode")
webService = xfer.GetToString()
xml=CreateObject("roXMLElement")
If xml.Parse(webService) then
print(xml.status.GetText())
End If
You are correct, I indeed had a error in code that was affecting this. It is working using your example, awesome!
Can you give me a brief explanation why the code on page 17 of the Brightscript Reference doesn’t work in this example? Shouldn’t I be able to use the logic there? Specifically, why can’t I do the following;
“brocker” wrote:
Can you give me a brief explanation why the code on page 17 of the Brightscript Reference doesn’t work in this example? Shouldn’t I be able to use the logic there? Specifically, why can’t I do the following;
print(xml.status[0])
The print statement expects its argument to be a string. xml.status[0] is not a string, it’s a roXMLElement. To get the body of the element as a string, you have to call its GetText() method.
So, from the user reference it makes it seem like you find the roXMLElement, and then the index would give you the respective positional value. It feels a bit like the documentation is inaccurate, the example about mid-page seems to be an roXMLElement as well and the sample code provides an index.
rsp.photos.photo[0]
will return “3131875696”
Is this something that perhaps is in BrightScript 3.0 only?
“BrightScript Reference Manual” wrote:
? rsp.photos.photo[0]
Will return an roXMLElement reference to the first photo (id=“3131875696”).
It says that line of code will return a roXMLElement whose value is the first photo element, which has an attribute id=“3131875696”. rsp.photos.photo[1] would return a roXMLElement whose value is the second photo element with id=“3131137552”.
To get the id attribute’s value as a string, you would say rsp.photos.photo[0]@id