Hay otras maneras de hacer esto, como usando ArrayCollation, sólo que quería hacer este post sobre lo fácil que es manejar contenido XML en Actionscript 3 (y el gran contraste en ese aspecto con manejar XML en Actionscript 2).
Para este artículo usaremos Flex Builder 2, disponible para descargar de la web de Adobe.
Bien, en AS3 existen las clases XMLDocument y XMLNode, que permiten trabajar con el XML como en AS2, pero no deberían usarse para nada. En su lugar se usa la clase XML.
Para convertir una cadena, objeto o cualquier otra cosa posible en XML, usamos la función global XML(expresión ).
Aquí viene lo bonito: si tenemos el xml de un rss, por ejemplo, http://www.cristalab.com/rss.php el XML se trabaja automáticamente como un Array de Objets, de tal manera que para referirnos a digamos el primer elemento será simplemente elXML.channel.item[0], sin tener que estar jugando con los condenados firstChild y ChildNodes. Pero lo mejor es que podemos usar un condicional en la propia referencia, y que se seleccionen sólo aquellos elementos para los que el condicional devuelve true. Me explico: Si queremos seleccionar todos los elementos del feed de clab cuyo título empiece por "C" el código sería simplemente:
Código :
elXml.channel.item.(title.substr(0,1)=="C")
Código :
elXml.channel.item.(title.substr(0,1)=="C")
Código :
clabRss.channel.item.(title.indexOf(evt.target.text)!=-1)
Código :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" initialize="init();">
<mx:Script>
<![CDATA[
public var clabRss:XML
public function init():void{
var req:URLRequest=new URLRequest("http://www.cristalab.com/rss.php");
var xmlLoader:URLLoader=new URLLoader();
xmlLoader.load(req);
xmlLoader.addEventListener(Event.COMPLETE,onLoadComplete)
}
public function onLoadComplete(event:Event):void{
clabRss=XML(event.target.data);
main.title=clabRss.channel.title
posts.labelField="title";
posts.dataProvider=clabRss.channel.item;
posts.selectedIndex=0;
titleTxt.text=clabRss.channel.item[0].title
description.text=clabRss.channel.item[0].description;
}
public function filter(evt:Event):void{
if(evt.target.text==""){
posts.dataProvider=clabRss.channel.item;
}else{
posts.dataProvider=clabRss.channel.item.(title.indexOf(evt.target.text)!=-1)
}
}
]]>
</mx:Script>
<mx:Panel id="main" width="100%" height="100%" layout="absolute">
<mx:Text y="10" text="Cargando..." fontWeight="bold" fontSize="36" id="titleTxt" height="168" left="10" right="10"/>
<mx:TextArea right="20" left="10" id="description" wordWrap="true" editable="false" bottom="75" top="150"/>
<mx:Button x="10" label="Copy Link" bottom="25" toolTip="{posts.selectedItem.link}" click="{System.setClipboard(posts.selectedItem.link)}"/>
</mx:Panel>
<mx:VBox height="100%">
<mx:Label text="Filtrar:" fontWeight="bold"/>
<mx:TextInput id="searchBox" width="100%" change="filter(event)"/>
<mx:List height="100%" id="posts" width="300" change="{titleTxt.text=posts.selectedItem.title;description.text=posts.selectedItem.description;}"></mx:List>
</mx:VBox>
</mx:Application>
Freddie® :
Código :
<?php
function plugClab($consulta){
//Parámetros de la db
$db="estudiantes";
$user="root";
$pass="";
$server="localhost";
//Conexion
$dbh=mysql_connect($server,$user,$pass) or die ("I cannot open the database because: " . mysql_error());
mysql_select_db($db);
$resultado=mysql_query($consulta,$dbh);
mysql_close($dbh);
return $resultado;
}
$sql = "SELECT * FROM calificaciones WHERE nombre LIKE '".$_GET["nombre"]."'";
$id=plugClab($sql);
$c=0;
while($calif[$c]=mysql_fetch_assoc($id)){
$c++;
}
mysql_free_result($id);
header('Content-type: text/xml; charset=utf-8');
?>
<?php echo "<?";?>
xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<calificaciones total="<?php echo ($c); ?>">
<?php
for ($i=0;$i<$c;$i++){
?>
<estudiante id= "<?php echo $calif[$i]["id"]; ?>" nombre="<?php echo $calif[$i]["nombre"]; ?>" email="<?php echo $calif[$i]["email"]; ?>" calificacion="<?php echo $calif[$i]["calificacion"]; ?>" />
<?php
}
?>
</calificaciones>
</rsp>
Maikel :
Código :
<mx:HTTPService id="feedRequest" url="http://weblogs.macromedia.com/mchotin/index.xml" useProxy="false"/>
Código :
feedRequest.lastResult.rss.channel.title
Código :
Error #2044: securityError no controlado: text=Error #2048: Violación de la seguridad Sandbox: http://try.flex.org/output/fBF704044E081438F300596AD7ADFED5D.swf no puede cargar datos desde http://www.cristalab.com/rss.php. at App/init() at App/___Application1_initialize() at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent() at mx.core::UIComponent/set processedDescriptors() at mx.core::Container/createComponentsFromDescriptors() at mx.core::Container/mx.core:Container::createChildren() at mx.core::UIComponent/initialize() at mx.core::Container/initialize() at mx.core::Application/initialize() at App/initialize() at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded() at mx.managers::SystemManager/::initializeTopLevelWindow() at mx.managers::SystemManager/::docFrameHandler()
D3N14M_blog :