Flash / Flex / ActionScript/XML/update

Материал из Web эксперт
Перейти к: навигация, поиск

After the replacement, the two <PASSWORD> elements have the same content:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var user1:XML = <USERDETAILS>
            <LOGIN>joe</LOGIN>
            <PASSWORD>linuxRules</PASSWORD>
          </USERDETAILS>;
          
        var user2:XML = <USERDETAILS>
            <LOGIN>ken</LOGIN>
            <PASSWORD>default</PASSWORD>
          </USERDETAILS>;
        
        trace(user1);
        trace(user2);
        user2.PASSWORD = user1.PASSWORD;
        trace(user1);
        trace(user2);
        
        trace(user1.PASSWORD[0] == user2.PASSWORD[0]); // Displays: true
        
        trace(user1.PASSWORD[0]



Changing the Contents of an Element

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
          <TITLE>ActionScript</TITLE>
          <AUTHOR>J, J</AUTHOR>
          <PUBLISHER>Books Ltd</PUBLISHER>
        </BOOK>
        
        novel.TITLE[0] = "New Title";
        
        trace(novel);
    }
  }
}



Content of an element can be changed using setChildren( )

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
          
        trace(novel);  
        novel.TITLE.setChildren("New Title");
        trace(novel);
    }
  }
}



Get and set the xml tag value

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var order:XML = <ORDER>
          <ITEM SKU="374">
            <NAME>Toy</NAME>
            <PRICE>39.99</PRICE>
            <QUANTITY>2</QUANTITY>
          </ITEM>
        </ORDER>
        
        var total:Number = 0;
        for each (var item:XML in order.*) {
          trace( item.QUANTITY
                        + " " + item.NAME + "(s)."
                        + " $" + item.PRICE + " each.\n");
          total += item.QUANTITY * item.PRICE;
        
        }
        
        trace("TOTAL: " + total);
        
        for each (var item:XML in order.*) {
          item.PRICE = 1;
        }
        
        trace(order);
        
    }
  }
}



Replace the element of one xml data with the PASSWORD element of another

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var user1:XML = <USERDETAILS>
            <LOGIN>joe</LOGIN>
            <PASSWORD>linuxRules</PASSWORD>
          </USERDETAILS>;
          
        var user2:XML = <USERDETAILS>
            <LOGIN>ken</LOGIN>
            <PASSWORD>default</PASSWORD>
          </USERDETAILS>;
        
        trace(user1);
        trace(user2);
        user2.PASSWORD = user1.PASSWORD;
        trace(user1);
        trace(user2);
    }
  }
}



Set attribute values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var movieList:XML = <movieList>
                                <listName>My favorite movies</listName>
                                <movie id="123">
                                    <title>Titus</title>
                                    <year>1999</year>
                                    <director>J T</director>
                                </movie>
                                <movie id="456">
                                    <title>Rushmore</title>
                                    <year>1998</year>
                                    <director>W A</director>
                                </movie>
                                <movie id="789">
                                    <title>Hall</title>
                                    <year>1977</year>
                                    <director>Woody Allen</director>
                                </movie>
                           </movieList>;
        movieList.movie[0].@id = 8675309;
    }
  }
}



The content of an element can be changed using the XML class"s instance method replace( ).

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var doc:XML = <DOC>
            <P ALIGN="CENTER">E4X is fun</P>
          </DOC>;
        
        doc.replace("P", <DIV>E4X is convenient</DIV>);
        
        trace(doc);
    }
  }
}



Update xml data by using the dynamic property

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
          
        trace(novel);  
          
        novel.AUTHOR = "H, E";
        novel.PUBLISHER = "S";
        
        trace(novel);
    }
  }
}



user2.PASSWORD[0]); // Displays: false

 
    }
  }
}



Use wild card and index to change the xml data value

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
          <TITLE>ActionScript</TITLE>
          <AUTHOR>J, J</AUTHOR>
          <PUBLISHER>Books Ltd</PUBLISHER>
        </BOOK>
        
        novel.*[1] = novel.*[1] + <AUTHOR>D, V</AUTHOR>;
        
        trace(novel);
    }
  }
}



Write new values to the XML

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var movieList:XML = <movieList>
                                <listName>My favorite movies</listName>
                                <movie id="123">
                                    <title>Titus</title>
                                    <year>1999</year>
                                    <director>J T</director>
                                </movie>
                                <movie id="456">
                                    <title>Rushmore</title>
                                    <year>1998</year>
                                    <director>W A</director>
                                </movie>
                                <movie id="789">
                                    <title>Hall</title>
                                    <year>1977</year>
                                    <director>Woody Allen</director>
                                </movie>
                           </movieList>;
        
        movieList.movie[2].director = "A K";
    }
  }
}