Player APIs (BrightScript & Ja...
...
BrightScript
Language Reference

XML Support

7min
brightscript provides xml support with two brightscript objects and a set of dedicated language features roxmlelement this object provides support for parsing, generating, and containing xml roxmllist this object is used to contain a list of roxmlelement instances dot operator the " " operators docid\ pl6ydtpgctydca5q2bw45 has the following features when used with xml objects when used with an roxmlelement instance, the " " dot operator returns an roxmllist instance of the child tags that match the dot operand if no tags match the operand, an empty list is returned when applied to an roxmllist instance, the " " dot operator aggregates the results of performing the above operation on each roxmlelement in the list when applied to xml, which is technically case sensitive, the " " dot operator is still case insensitive if you wish to perform a case sensitive xml operation, use the member functions of the roxmlelement / roxmllist objects attribute operator the “@” attribute operator can be used with an roxmlelement instance to return a named attribute though xml is case sensitive, the attribute operator is always case insensitive if the attribute operator is used with an roxmllist instance, it will only return a value if that list contains exactly one element examples \<?xml version="1 0" encoding="utf 8" ?> \<rsp stat="ok"> \<photos page="1" pages="5" perpage="100" total="500"> \<photo id="3131875696" owner="21963906\@n06" secret="f248c84625" server="3125" farm="4" title="vny 16r" ispublic="1" isfriend="0" isfamily="0" /> \<photo id="3131137552" owner="8979045\@n07" secret="b22cfde7c4" server="3078" farm="4" title="hoot" ispublic="1" isfriend="0" isfamily="0" /> \<photo id="3131040291" owner="27651538\@n06" secret="ae25ff3942" server="3286" farm="4" title="172 • 365 someone once told me " ispublic="1" isfriend="0" /> \</photos> \</rsp> given the xml in the above example xml file, then the following code will return an roxmllist instance with three entries rsp=createobject("roxmlelement") rsp parse(readasciifile("example xml")) ? rsp photos photo the following will return an roxmlelement reference to the first photo (id=" 3131875696 ") ? rsp photos photo\[0] the following will return an roxmllist reference containing the \<photos> tag ? rsp photos the following will return the string “100” rsp photos\@perpage you can use the roxmlelement gettext() method to return an element’s text for example, if the variable \<booklist> contains the element \<book lang=eng>the dawn of man\</book> , then the following code will print the string “the dawn of man” print booklist book gettext() alternatively, using the attribute operator will print the string “eng” print booklist book\@lang flikr code clip rem rem interestingness rem pass an (optional) page of value 1 5 to get 100 photos rem starting at 0/100/200/300/400 rem rem returns a list of "interestingness" photos with 100 entries rem function getinterestingnessphotolist(http as object, page=1 as integer) as object &#x9;print "page=";page http seturl("http //api flickr com/services/rest/?method=flickr interestingness getlist\&api key=yourkeygoeshere\&page="+mid(stri(page),2)) xml=http gettostring() rsp=createobject("roxmlelement") if not rsp parse(xml) then stop return helperphotolistfromxml(http, rsp photos photo) 'rsp getbody() peek() getbody()) end function function helperphotolistfromxml(http as object, xmllist as object, owner=invalid as dynamic) as object photolist=createobject("rolist") for each photo in xmllist photolist push(newphotofromxml(http, photo, owner)) end for return photolist end function rem rem newphotofromxml rem rem takes an roxmlelement object that is an \<photo> \</photo> rem returns an brs object of type photo rem photo gettitle() rem photo getid() rem photo geturl() rem photo getowner() rem function newphotofromxml(http as object, xml as object, owner as dynamic) as object photo = createobject("roassociativearray") photo http=http photo xml=xml photo owner=owner photo gettitle=function()\ return m xml\@title\ end function photo getid=function()\ return m xml\@id\ end function photo getowner=pgetowner photo geturl=pgeturl return photo end function function pgetowner() as string &#x9;if m owner<>invalid return m owner &#x9;return m xml\@owner end function function pgeturl() as string &#x9;a=m xml getattributes() &#x9;url="http //farm"+a farm+" static flickr com/"+a server+"/"+a id+" "+a secret+" jpg" &#x9;return url end function