+ Reply to Thread
Results 1 to 5 of 5

Looping attraverso una XMLList Collection

This is a discussion on Looping attraverso una XMLList Collection within the FLEX builder 3 forums, part of the Flash Italiano category; Salve a tutti avrei una domanda, ho una XMLList Collection fatta così: HTML Code: <listacampi> <campo id= "0" > <localita> ...

  1. #1
    Junior Member Settled In teocomi is on a distinguished road
    Join Date
    Mar 2008
    Posts
    28
    Rep Power
    0

    Looping attraverso una XMLList Collection

    Salve a tutti avrei una domanda, ho una XMLList Collection fatta così:
    HTML Code:
    <listacampi>
        <campo id="0">
            <localita>San Lazzaro</localita>
            <comune>Bologna</comune>
        </campo>
        <campo id="2">
            <localita>bologna</localita>
            <comune>Bologna</comune>
         </campo>
        <campo id="4">
            <localita>bologna</localita>
            <comune>Bologna</comune>
         </campo>
    </listacampi>
    come faccio adapportare modifiche/eliminare per esempio il campo con id=2?

    la soluzione che ho adottato al momento è fare un ciclo in cui trasformo ogni item della XMLListCollection in XML e verifico se l'id è uguale a quello che cerco, così scopro l'index del campo e riesco ad apportarci le modifiche (listData è l'XMLListCollection):

    PHP Code:
     var l:int listData.length;
                        var 
    i:int;
                        var 
    xmlList:XMLList;
                        var 
    index:int;
                    for (
    i=0i<li++) {
                                
    xml = new XML(listData.getItemAt(i));
                                if (
    xml.@id == ID) {
                                
    index =i;
                                
    i=l;
                            }
                } 
    Grazie a tutti in anticipo!
    Last edited by teocomi; 31-08-09 at 15:20.

  2. #2
    Moderator Moving My Stuff To The FlepStudio OrientExpress is on a distinguished road OrientExpress's Avatar
    Join Date
    Aug 2009
    Posts
    1,277
    Rep Power
    5

    Re: Looping attraverso una XMLList Collection

    Io farei in questo modo:
    Code:
    var ID:int=4;
    var nuova_localita:String="Lame";
    var original_xml:XML;
    loadXML("test.xml");
    
    function loadXML(file:String):void
    {
        var request:URLRequest=new URLRequest(file);
        var loader:URLLoader=new URLLoader();
        loader.addEventListener(Event.COMPLETE,completeXMLHandler);
        try 
        {
            loader.load(request);
        } 
        catch(error:Error) 
        {
            trace('Impossible to load requested document.');
        }
    }
    
    function completeXMLHandler(event:Event):void
    {
        var loader:URLLoader=URLLoader(event.target);
        original_xml=new XML(loader.data);
        //trace(original_xml);
        //trace("----------------------------------");
        
        var myXML:XMLDocument=new XMLDocument();
        myXML.ignoreWhite=true;
        myXML.parseXML(original_xml.toXMLString());
        var node:XMLNode=myXML.firstChild;
            
        var categories:int=int(node.childNodes.length);
        for(var i:int=0;i < categories;i++)
        {
            var id:int=int(node.childNodes[i].attributes["id"]);
            
            if(id==ID)
            {
                original_xml.campo[i].localita=nuova_localita;
            }
        }
        
        trace(original_xml);
    }
    ottenendo questo nuovo XML:
    HTML Code:
    <listacampi>
      <campo id="0">
        <localita>San Lazzaro</localita>
        <comune>Bologna</comune>
      </campo>
      <campo id="2">
        <localita>bologna</localita>
        <comune>Bologna</comune>
      </campo>
      <campo id="4">
        <localita>Lame</localita>
        <comune>Bologna</comune>
      </campo>
    </listacampi>
    In questo caso ho cambiato bologna con Lame

  3. #3
    Junior Member Settled In teocomi is on a distinguished road
    Join Date
    Mar 2008
    Posts
    28
    Rep Power
    0

    Re: Looping attraverso una XMLList Collection

    perfetto, grazie mille! ma quindi tu mi consigli di usare un XMLDocument? perchè negli esempi dell'Adobe ho sempre visto usare XMLListColletions..
    E' possibile popolare una DataGrid con un XMLDocument?

    Grazie ancora!

  4. #4
    Moderator Moving My Stuff To The FlepStudio OrientExpress is on a distinguished road OrientExpress's Avatar
    Join Date
    Aug 2009
    Posts
    1,277
    Rep Power
    5

    Re: Looping attraverso una XMLList Collection

    Io sono abituato ad usare XMLDocument perchè ho imparato da Flep e mi ci trovo.
    Di sicuro il metodo Adobe è indiscutibile, però ad esempio ho notato che usando la XMLList se cambi i nomi dei tags del file XML non funziona più niente.

    Comunque con l' esempio che ti ho fatto puoi sempre traspostarlo sulla XMLList credo

  5. #5
    Junior Member Settled In teocomi is on a distinguished road
    Join Date
    Mar 2008
    Posts
    28
    Rep Power
    0

    Re: Looping attraverso una XMLList Collection

    grazie, adesso provo!

+ Reply to Thread

Similar Threads

  1. Replies: 78
    Last Post: 28-02-10, 07:36
  2. Replies: 109
    Last Post: 28-02-10, 07:32
  3. è sbagliato usare più xmlList?
    By nuovobuio in forum Actionscript 3.0 base
    Replies: 0
    Last Post: 11-12-09, 20:09
  4. e possibile richiamare una classe attraverso una variabile Object?
    By Marcello Kabora in forum Actionscript 3.0 avanzato
    Replies: 9
    Last Post: 29-10-09, 08:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts