Flash / Flex / ActionScript/XML/toString

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Converting an Attribute to a String

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0141182806">
            <TITLE>Actionscript</TITLE>
            <AUTHOR>J, K</AUTHOR>
            <PUBLISHER>Penguin Books Ltd</PUBLISHER>
          </BOOK>;
        
        
        trace(novel.@ISBN.toString(  ));  // 
    }
  }
}



Converting an XML Element to a String

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var novel:XML = <BOOK ISBN="0141182806">
            <TITLE>Actionscript</TITLE>
            <AUTHOR>J, K</AUTHOR>
            <PUBLISHER>Penguin Books Ltd</PUBLISHER>
          </BOOK>;
        
        
        trace(novel.toString(  ));  // Displays:
        trace(novel.TITLE.toString(  ));  // Displays: Actionscript
        
        trace(novel.TITLE.toXMLString(  ));  // Displays: <TITLE>Actionscript</TITLE>
        
    }
  }
}



Difference between toString and toXMLString

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      var p:XML = <p>&></p>;
        trace(p.toString(  ));     // Displays: &>
        trace(p.toXMLString(  ));  // Displays: <p>&amp;&gt;</p>
    }
  }
}



If the XML object in question contains only a single node only the text value of the node will be returned

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var movieList:XML = <listName>My favorite movies</listName>;
        trace(movieList.toString());
    }
  }
}



Printing Pretty

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:XML = <stooges>
                             <moe /><curly><larry>
            </larry>
                                   </curly>
                   </stooges>;
        XML.prettyPrinting = false;
        trace(example);
        
        
        XML.prettyPrinting = true;
        trace(example);
        
    }
  }
}



Setting the Number of Spaces per Indentation

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var example:XML = <stooges>
                             <moe /><curly><larry>
            </larry>
                                   </curly>
                   </stooges>;
        
        XML.prettyIndent = 0;
        trace(example);
        
        XML.prettyIndent = 8;
        trace(example);
    }
  }
}



The toXMLString() method always return the full XML tag and all values contained within an element.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var meal:XML = <meal>
                           <name>Dinner</name>
                           <course number="1">
                               <item>Salad</item>
                           </course>
                           <course number="2">
                               <item>Potatoes</item>
                               <item temperature="Medium Rare">Steak</item>
                           </course>
        
                       </meal>;
        
        trace(meal.name); // Displays: Dinner
        trace(meal.course[0]); // Displays: <course number="1">
        
        trace(meal.course[1].item[0].toXMLString()); // Displays: <item>Potatoes</items>
        trace(meal.course[1].item[1].toString()); // Displays: Steak
        trace(meal.course[1].item[1].@temperature.toXMLString()); 
        trace(meal..item.toString());
        
    }
  }
}