| 
					by 
					kirupa  |  17 July 2007
 Attributes are interesting little creatures - far more different than the 
children nodes you dealt with in the
previous page. Let's learn 
how to tame them in this page. So far, we have primarily dealt with reading nodes/elements and their nested information. 
Attributes are different in that they are information stored directly on the 
node itself. As such, the approach for accessing that information is a little 
different.
 In our XML file, the attribute is the ISBN information located directly on 
the Book node: 
  Let's look at the AS used to extract that information: 
	function ParseBooks(bookInput:XML):void
	{ 
		trace("XML 
		Output"); trace("------------------------");
		  var 
		bookAttributes:XMLList
		= 
		bookInput.Book.attributes();
		  for each
		(var
		bookISBN:XML
		in bookAttributes)
		{ 
			trace(bookISBN); } } If you overwrite your earlier ParseBooks function with the above function and 
test your application (Ctrl + Enter), you will see the ISBN numbers printed. 
Let's look at the one line of code that makes this work: 
	var bookAttributes:XMLList
	= bookInput.Book.attributes(); To access the list of attributes, I call the attributes() method on our Book 
node. The attributes information, if available, is returned in the form of...you 
guessed it...an XMLList! I then iterate through that list, just like before, and print 
out that information. This approach, like our earlier XML's children() approach, prints out all of 
the matching data. In this case, all attribute values are printed out. That is 
not a problem because we only have one attribute per Book node, but if you had 
multiple attributes, all of them would be printed. Let's look at two ways of 
keeping track of them. The first approach is similar to what you already used earlier. You check for 
the attribute's name and see if it matches what you are looking for: 
	function ParseBooks(bookInput:XML):void
	{ 
		trace("XML 
		Output"); trace("------------------------");
		  var 
		bookAttributes:XMLList
		= bookInput.Book.attributes();
		  for each
		(var
		bookISBN:XML
		in bookAttributes)
		{ 
			if (bookISBN.name()
			== "ISBN")
			{ 
				trace(bookISBN); } } } This works because the name() method returns 
the name of node. The name() method does not distinguish between children, 
attributes, parents, etc. It only cares about what the name of the XML object 
is, so the similarity in how the name() object is used for accessing the 
attribute's name and the node's name is intentional. The second approach is where you filter based on the attribute name when 
generating your XMLList itself: 
	function ParseBooks(bookInput:XML):void
	{ 
		trace("XML 
		Output"); trace("------------------------");
		  var 
		bookAttributes:XMLList
		= bookInput.Book.attribute("ISBN");
		  for each
		(var
		bookISBN:XML
		in bookAttributes)
		{ 
			trace(bookISBN); } } In this approach, you use the attribute() method where you pass in the name 
of the attribute you are looking for: 
	var bookAttributes:XMLList
	= bookInput.Book.attribute("ISBN"); Your bookAttributes XMLList object will only contain a list of XML objects 
that contain attributes matching the ISBN name. This approach avoids you having 
to iterate through a larger list of XML objects and checking each name manually.  Onwards to the next page! |