Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
jkard
Visitor

XML Parsing Error - roXMLElement

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


The XML returns

<result>
<status>success</status>
<regCode>29AX3</regCode>
<retryInterval>30</retryInterval>
<retryDuration>900</retryDuration>
</result>


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"

What am I doing wrong?

Thanks!
Jay
0 Kudos
8 REPLIES 8
TheEndless
Channel Surfer

Re: XML Parsing Error - roXMLElement

result is the root node, so you should use xml.status.GetText() instead.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
brocker
Visitor

Re: XML Parsing Error - roXMLElement

"TheEndless" wrote:
result is the root node, so you should use xml.status.GetText() instead.


Thanks Endless,

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?

Thanks!
Bud
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: XML Parsing Error - roXMLElement

"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
0 Kudos
brocker
Visitor

Re: XML Parsing Error - roXMLElement

Thanks Chris,

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;

print(xml.status[0])


Thanks!
Bud
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: XML Parsing Error - roXMLElement

"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.
0 Kudos
jkard
Visitor

Re: XML Parsing Error - roXMLElement

Thanks Chris,

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?

Thanks!
Jay
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: XML Parsing Error - roXMLElement

"jkard" wrote:
rsp.photos.photo[0]
will return "3131875696"


That's not what the document says.

"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
0 Kudos
jkard
Visitor

Re: XML Parsing Error - roXMLElement

Thanks Chris,

I figured it was my misunderstanding, but just wanted to be sure.

Thanks for clarifying
Jay
0 Kudos