package com.ericfeminella.xml
{
/**
*
* All static utility class which provides an API for working
* with and modifyinmg XML attributes
*
* @see XMLList
*
*/
public final class XMLAttributeUtil
{
/**
*
* Retrieves all attribute values for a specified XML object
* and returns the values as an Array
*
* @param the XML source to query
* @return an array of attribute values
*
*/
public static function getAttributeValues(source:XML) : Array
{
var attributesList:XMLList = source..@*;
var attributeValues:Array = [];
var n:int = attributesList.length();
for (var i:int = 0; i < n; i++)
{
attributeValues.push( attributesList[i] );
}
return attributeValues;
}
/**
*
* Retrieves all attribute names for a specified XML object
* and returns the names as a String Array
*
* @param the XML source to query
* @return String array of attribute names
*
*/
public static function getAttributeNames(source:XML) : Array
{
var attributesList:XMLList = source..@*;
var attributeNames:Array = [];
var n:int = attributesList.length();
for (var i:int = 0; i < n; i++)
{
attributeNames.push( attributesList[i].name() );
}
return attributeNames;
}
/**
*
* Retrieves an attribute value for a specified XML object
* via the attribute name
*
* @param the XML source to query
* @param the name of the attribute to query on
* @return an XMLList of attribute values
*
*/
public static function getAttributeValueByName(source:XML, attributeName:String) : String
{
return source.attribute( attributeName ).toString();
}
/**
*
* Retrieves all attribute values for a specified XML object
* via a specific attribute name
*
* @param the XML source to query
* @param the name of the attribute to query on
* @return an Array of attribute values for the specified attribute
*
*/
public static function getAttributeValuesByName(source:XML, attributeName:String) : Array
{
var attributesList:XMLList = source..@*;
var attributeValues:Array = [];
var n:int = attributesList.length();
for (var i:int = 0; i < n; i++)
{
if ( attributesList[i].name() == attributeName )
{
attributeValues.push( attributesList[i] );
}
}
return attributeValues;
}
/**
*
* Retrieves an attribute name for a specified XML
* object via the attributes value
*
* @param the XML source to query
* @param the value of the attribute to query on
* @return the attribute name for the specified value
*
*/
public static function getAttributeNameByValue(source:XML, attributeValue:*) : String
{
var attributesList:XMLList = source..@*;
var n:int = attributesList.length();
var result:String = null;
for (var i:int = 0; i < n; i++)
{
if ( attributesList[i] == attributeValue )
{
result = attributesList[i].name();
break;
}
}
return result;
}
}
}