Forum Discussion

destruk's avatar
destruk
Streaming Star
15 years ago

XML troubles with parsing

Maybe I'm just entirely stupid, or perhaps it will only work one way and I haven't understood what that single method is yet. I'm asking for a little help with this.
I have an xml file - from the video player example, and I'm using this code:

rss=CreateObject("roUrlTransfer")
rss.SetUrl("http://testurlxml.com/file.xml")
xml=rss.GetToString()
rss=CreateObject("roXMLElement")
rss.Parse(xml)

What this should be doing, as far as I can understand, is it creates the urltransfer object which allows it to access files from the internet
Then it sets the url to the file I want to retrieve from the internet
Then it puts the entire contents of that file into a string variable I have named "xml"
Next it creates the roxmlelement object which I have named "rss"

As far as I can tell, rss.parse(xml) should create an associative array of each 'item' group in the xml file.
I can print xml and it displays the entire xml file in the debugger, so that part should be ok
I can print rss.item and it displays ROT:roxmlElement three times, so that should be ok, I think, too, as there are three items in it
I can also print rss.item[0] and it displays ROT:xmlElement once, which is fine as well, as that is item 0, the first element of the array.

Now, using the video player example, how would I be able to reference the "title" field directly? Can I use something like "print rss.item[0].title" ? Seems everything I try to get an actual value of a specific field is failing for me.

4 Replies

  • There is some sample code for using roXMLElement in the BrightScript Reference Manual starting on p46. The method you're looking for to get the text within a tag is GetText()

     print rss.item[0].title.GetText()
  • destruk's avatar
    destruk
    Streaming Star
    ok, that works fine RokuChris. 🙂
    Now, I think I have just one more little piece to go and then I can be on my way.
    my xml file has three entries for now.

    endcount=rss.item.count()-1

    ShowList=CreateObject("roAssociativeArray")

    ShowList = [{
    EpisodeNumber:"1",
    ShortDescriptionLine1:"Show #1",
    ShortDescriptionLine2:"Short Description for Show #1",
    }
    {
    EpisodeNumber:"2",
    ShortDescriptionLine1:"Show #2",
    ShortDescriptionLine2:"Short Description for Show #2",
    }
    {
    EpisodeNumber:"3",
    ShortDescriptionLine1:"Show #3",
    ShortDescriptionLine2:"Short Description for Show #3",
    }
    ]


    for z=0 to endcount
    showlist.EpisodeNumber=rss.item.EpisodeNumber.GetText()
    showlist.ShortDescriptionLine1=rss.item.ShortDescriptionLine1.GetText()
    showlist.ShortDescriptionLine2=rss.item.ShortDescriptionLine2.GetText()
    showlist.HDPosterURL=hdstring
    showlist.SDPosterURL=sdstring
    end for
    Return showList

    This code works - however, if I remove the initial declaration/population of ShowList then I get mismatch errors for the episodenumber.gettext line. How can I simply, manually populate and expand the array as necessary?
  • You probably want to start with an empty array and build it up as you step through the XML.

    showList = []
    for each item in rss.item
    showList.Push({
    ShortDescriptionLine1: item.title.GetText()
    ...
    })
    next