Soporte XML
BrightScript proporciona compatibilidad con XML mediante dos objetos BrightScript y un conjunto de características dedicadas del lenguaje:
- roXMLElement: Este objeto proporciona compatibilidad para analizar, generar y contener XML.
- roXMLList: Este objeto se utiliza para contener una lista de instancias de roXMLElement.
Operador de punto
El Operador de puntoDot Operator "." tiene las siguientes características cuando se utiliza con objetos XML:
- Cuando se utiliza con una instancia de roXMLElement, el operador de punto "." devuelve una instancia de roXMLList de las etiquetas secundarias que coinciden con el operando del punto. Si ninguna etiqueta coincide con el operando, se devuelve una lista vacía.
- Cuando se aplica a una instancia de roXMLList, el operador de punto "." agrega los resultados de realizar la operación anterior en cada roXMLElement de la lista.
- Cuando se aplica a XML, que técnicamente distingue entre mayúsculas y minúsculas, el operador de punto "." sigue sin distinguir entre mayúsculas y minúsculas. Si desea realizar una operación XML sensible a mayúsculas y minúsculas, utilice las funciones miembro de los objetos roXMLElement/roXMLList.
Operador de atributos
El operador de atributos “@” se puede utilizar con una instancia de roXMLElement para devolver un atributo nombrado. Aunque XML distingue entre mayúsculas y minúsculas, el operador de atributos nunca distingue entre mayúsculas y minúsculas. Si el operador de atributos se utiliza con una instancia de roXMLList, solo devolverá un valor si esa lista contiene exactamente un elemento.
Ejemplos
<?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>Dado el XML del archivo example.xml anterior, el siguiente código devolverá una instancia de roXMLList con tres entradas:
rsp=CreateObject("roXMLElement")
rsp.Parse(ReadAsciiFile("example.xml"))
? rsp.photos.photoLo siguiente devolverá una referencia roXMLElement a la primera foto (id="3131875696"):
? rsp.photos.photo[0]Lo siguiente devolverá una referencia roXMLList que contiene la etiqueta <photos>:
? rsp.photosLo siguiente devolverá la cadena “100”:
rsp.photos@perpagePuede utilizar el método roXMLElement.GetText() para devolver el texto de un elemento: por ejemplo, si la variable <booklist> contiene el elemento <book lang=eng>El amanecer del hombre</book>, entonces el siguiente código imprimirá la cadena “The Dawn of Man”.
Print booklist.book.gettext()Alternativamente, usar el operador de atributos imprimirá la cadena “eng”.
print booklist.book@langClip de código de Flikr
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
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
if m.owner<>invalid return m.owner
return m.xml@owner
End Function
Function pGetURL() As String
a=m.xml.GetAttributes()
url="http://farm"+a.farm+".static.flickr.com/"+a.server+"/"+a.id+"_"+a.secret+".jpg"
return url
End Function