Flex: Filtrar datos XML a mostrar en un Datagrid

Este es otro post escrito a partir de un mensaje en la lista: Madeinflex.
Un usuario de la lista (Chusete) preguntaba, como se puede filtrar un XML recibido desde un HTTPService para que solo se muestre una parte de dicho XML en un Datagrid.

La solucion estriba en aplicar filtros de búsqueda al elemento XML devuelto. Por ejemplo las funciones children y
descendants
ayudan. Si tenemos el siguiente XML:

<data>
  <producto name="opcion1">
    <label>telefono</label>
    <precios_antes_junio>22</precios_antes_junio>
    <precios_antes_junio_IVA>25,52</precios_antes_junio_IVA>
    <precios_despues_junio>56</precios_despues_junio>
    <precios_despues_junio_IVA>56,52</precios_despues_junio_IVA>

    <telefonica>000</telefonica>
  </producto>
  <producto name="opcion2">
    <label>television</label>
    <precios_antes_junio>aa</precios_antes_junio>
    <precios_antes_junio_IVA>sss</precios_antes_junio_IVA>
    <precios_despues_junio>fff</precios_despues_junio>

    <precios_despues_junio_IVA>ttt</precios_despues_junio_IVA>
    <telefonica>uuuu</telefonica>
  </producto>
</data>

Si queremos buscar los hijos del documento elemento (el elemento raiz) del XML cuyo atributo name sea: opcion1:

myXml.children().(@name=='opcion1')

Si queremos buscar cualquier elemento descendiente del documento elemento con nombre producto y que tengan un atributo name con valor: opcion1:

myXml.descendants('producto').(@name=='opcion1')

En nuestro ejemplo, ambas consultas devolveran lo mismo, porque solo hay un nivel de elementos con nombre producto.

Un ejemplo completo de aplicación Flex, que se conecta a un WebService para obtener el XML del ejemplo y mostrar en el DataGrid solo uno de los dos productos, podría ser el siguiente:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
         creationComplete="httpProductos.send()">

        <mx:Script>
                <![CDATA[
                        [Bindable]
                        public var datos_productos : XML;
                ]]>
        </mx:Script>

<mx:HTTPService id="httpProductos"
        url="http://192.168.1.133:3000/main/prueba"
        useProxy="false" method="GET" result="datos_productos = new
XML(event.message.body);" />

        <mx:DataGrid x="0" y="0" width="100%" height="100%"
id="dgDatos"
                dataProvider="{datos_productos.children().
(@name=='opcion2')}" >
                <mx:columns>
            <mx:DataGridColumn dataField="label"
                                           headerText="Label"
                                           width="400" />
                </mx:columns>
        </mx:DataGrid>
</mx:Application> 

Todavía no hay comentarios

Enviar un comentario nuevo

El contenido de este campo se mantiene privado y no se mostrará públicamente.
  • Etiquetas HTML permitidas: <a> <blockquote> <br> <cite> <code> <dd> <div> <dl> <dt> <em> <h1> <h2> <h3> <h4> <h5> <h6> <hr> <img> <li> <ol> <p> <pre> <span> <strong> <swf> <table> <tbody> <td> <th> <tr> <ul>
    Allowed Style properties: background-color, background-image, border, border-bottom, border-bottom-color, border-bottom-style, border-bottom-width, border-color, border-left, border-left-color, border-left-style, border-left-width, border-right, border-right-color, border-right-style, border-right-width, border-spacing, border-style, border-top, border-top-color, border-top-style, border-top-width, border-width, color, direction, font, font-family, font-size, font-style, font-variant, font-weight, height, left, line-height, list-style-type, margin, margin-bottom, margin-left, margin-right, margin-top, padding, padding-bottom, padding-left, padding-right, padding-top, right, text-align, text-decoration, top, width
  • Syntax highlight code surrounded by the {syntaxhighlighter OPTIONS}...{/syntaxhighlighter} tags.
  • E-Mail addresses are hidden with reCAPTCHA Mailhide.

Más información sobre opciones de formato

CAPTCHA
Esta pregunta es para comprobar si eres un ser humano y evitar el envío automático desde sistemas de spam