Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Friday, March 5, 2010

AS3: Does my xml doc contain a attribute

This is a simple check which increases the ease of using xml.

if you have a xml file like so:

<root_node some_attribute="">
</root_node>

in AS3 you can decide if a attribute exist by using the lenght() method. So code using the xml would be:

var my_xml:XML = some_function_to_get_xml();

if( my_xml.@some_attribute.length() != 0 ) {
trace("Yup, some_attribute exists");
}

if( my_xml.@some_non_existant_attribute.length() != 0) {
trace("This will not be printed since attribute does not exist");
}

Note it is easy to make a mistake and forget to use length as a method i.e. "length()" and use it as variable i.e. just "length" which will not work.

Tuesday, December 22, 2009

AS3: Embedding XML files

If you have a XML file called myfile.xml. Code to include this would be:

[Embed(source="mfile.xml", mimeType="application/octet-stream")]
[Bindable]
private var my_file:Class;

function some_func():void {
var my_xml:XML = XML(new my_file);
}


my_xml now contains valid XML. You can now access stuff it like normal XML variables.

Got this from the comments in this post: http://dispatchevent.org/roger/embed-almost-anything-in-your-swf/