Posts Tagged “XML”

Dynamically listing the properties of a class is pretty cool and useful. There are many things you could do by generating a list of existing properties. One example is you can make your class instances represent themselves dynamically in a custom format of your choosing. Like how "toString()" is use with the Array class or how Point is represented when toString() is called from it. Going further, listing current properties could be used to save the current values of the class as a "save point".

In the Flex framework, there is ObjectUtil.getClassInfo().properties, but I feel is more useful to just use the XML returned from the describeType method. I like using decribeType for both plain AS3 or when using the Flex framework because its accessible in both.
Since the return of describeType is XML I have more options with E4X filtering/Regular Expressions, than using getClassInfo(). With describeType I can get info on methods as well but to keep things simple I'm only talking about accessor properties in this post. Using the "listProperties" function, I am listing the properties between 3 different classes. This is a function I wrote to show how you can make your own way to use the XML of decribeType.

In the example SWF below, there are 3 classes under inspection. The Helicopter class is a custom class extending Sprite. By setting the parameters of listProperties you can list the properties of Helicopter, or all of the properties it has by being subclassed from Sprite. The other classes are a TextField and a Sprite class. Changing the options lists a different set of properties based on type of Class of properties listed and what class its properties are declared by.

Get Adobe Flash player


Read the rest of this entry »

Vote in HexoSearch

Comments 1 Comment »

When I'm writing a script to read XML data, I personally like the identification of node names and attributes to be case-insensitive. Its one less thing to worry about when others are modifying the XML data that my script will have to read and render, and I think they feel modifying the XML to be more user-friendly this way. :)

Here, the example scripts pull a list of all the nodes with the name "MEDIA".
Using Regular Expressions, you can identify each "MEDIA" node regardless of its letter case.

 
var doc:XML=
<DATA>
	<CATEGORY>
		<MEDIA>A.jpg</MEDIA>
		<MEDIA>B.jpg</MEDIA>
	</CATEGORY>
	<CATEGORY>
		<MEDIA>C.jpg</MEDIA>
	</CATEGORY>
	<CATEGORY>
		<MEDIA>D.jpg</MEDIA>
	</CATEGORY>
	<CATEGORY>
		<MEDIA>E.jpg</MEDIA>
	</CATEGORY>
</DATA>;
 
var findName:String="media";
trace(doc..*.(new RegExp(findName,"i")["test"](name())).toXMLString());
 

:( "Error #1069: Property lowerCase not found on QName"

 
trace(doc..*.(name().lowerCase()=="media").toXMLString());
 

:( Letter case must be same for this to work.

 
trace(doc..*.(name()=="MEDIA").toXMLString());
 

Used a regular expression to do case-insensitive filter
:| Works, letter case does not matter...but the expression is too "hardcoded".

 
trace(doc..*.(/media/i.test(name())).toXMLString());
 

:-? Works, but compiler does not like it:
"Warning: 3594: test is not a recognized method of the dynamic class RegExp."
This is an attempt to allow variables and expressions.

 
var findName:String="media";
trace(doc..*.(new RegExp(findName,"i").test(name())).toXMLString());
 

:jumping: Works!
"[" and "]" keeps the compiler from warning when using the RegExp "test()" method.
Now letter case does not matter, and RegExp constructor allows variables or expressions.

 
var findName:String="media";
trace(doc..*.(new RegExp(findName,"i")["test"](name())).toXMLString());
 
Vote in HexoSearch

Comments 3 Comments »

The XML examples represent a book in a library. However the book has several bookworms in it.
Using E4X we can find these bookworms.

 
<library>
	<book>
<page id="one"/>
		<worm name="Eddy" />
<page id="two"/>
<page id="three">
			<worm name="Lisa"/>
			<worm name="Pete" />
		</page>
<page id="four"/>
	</book>
</library>
 

There are some E4X operators and XML methods to help use do this when combined together:

.. Access all descendants of the XML object.
. Access a property of an Object or XML.
* Wildcard, match any part of the XML.
childIndex() Index number of an XML object relative to its parent node.
name() Name of the XML node.

Read the rest of this entry »

Vote in HexoSearch

Comments 9 Comments »

Thanks for visiting www.keith-hair.net