| XML.attributes Property | Flash 5 |
| an object whose properties store element attributes | read/write |
The attributes property stores the names and values of attributes defined by theNode, where theNode can be an XML or XMLnode instance. For example, the ALIGN attribute of this P tag:
<P ALIGN="CENTER">this is a paragraph</P>
is accessed using theNode.attributes.ALIGN or theNode.attributes["ALIGN"]. If the P tag is the only tag in our XML source, we can access the ALIGN attribute as follows:
// Create an XML object hierarchy
myDoc = new XML('<P ALIGN="CENTER">this is a paragraph</P>');
// Access the ALIGN attribute. Displays: "CENTER"
trace(myDoc.firstChild.attributes.ALIGN);
// Set the ALIGN attribute
myDoc.firstChild.attributes.ALIGN = "LEFT";
The attributes property is itself an object. We can add new properties to the attributes object, thereby adding new attributes to theNode, as follows:
// Add a CLASS attribute to the P tag myDoc.firstChild.attributes.CLASS = "INTRO"; // firstChild now represents the XML source: // <P ALIGN="CENTER" CLASS="INTRO">this is a paragraph</P>
Because attributes is not an array, it doesn't contain a length property to indicate the number of properties/attributes it includes. Instead, we can access all the attributes defined on an element using a for-in loop:
var count = 0;
for (var prop in theNode.attributes) {
trace("attribute " + prop + " has the value " + theNode.attributes[prop]);
count++;
}
trace("The node has " + count + " attributes.");
If the XML element represented by theNode has no attributes, attributes is an empty object with no properties, and the preceding example would indicate zero attributes.