Flash / Flex / ActionScript/XML/toString
Содержание
- 1 Converting an Attribute to a String
- 2 Converting an XML Element to a String
- 3 Difference between toString and toXMLString
- 4 If the XML object in question contains only a single node only the text value of the node will be returned
- 5 Printing Pretty
- 6 Setting the Number of Spaces per Indentation
- 7 The toXMLString() method always return the full XML tag and all values contained within an element.
Converting an Attribute to a String
<source lang="java">
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( )); // } }
}
</source>
Converting an XML Element to a String
<source lang="java">
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> } }
}
</source>
Difference between toString and toXMLString
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){var p:XML =
&>
;trace(p.toString( )); // Displays: &>trace(p.toXMLString( )); // Displays:
&>
} }
}
</source>
If the XML object in question contains only a single node only the text value of the node will be returned
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var movieList:XML = <listName>My favorite movies</listName>; trace(movieList.toString()); } }
}
</source>
Printing Pretty
<source lang="java">
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); } }
}
</source>
Setting the Number of Spaces per Indentation
<source lang="java">
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); } }
}
</source>
The toXMLString() method always return the full XML tag and all values contained within an element.
<source lang="java">
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()); } }
}
</source>