Get Flex XML attribute name

来源:互联网 发布:java开发数据库面试题 编辑:程序博客网 时间:2024/06/08 09:14

Theattributes()method returns anXMLListobject with all the data from the attributes contained within anXMLobject. You can call thename()method for each attribute in theXMLListto retrieve the name of the attribute as a string. You can then use that value as a parameter, which you can pass to theattribute()method of theXMLobject to retrieve the value of the attribute. The following example illustrates how this works:

  var author0:XML = xml.children()[0].children()[1].children()[0];
  var attributes:XMLList = author0.attributes();
  var attributeName:String;
  for(var i:uint = 0; i < attributes.length(); i++) {
    attributeName = attributes[i].name();
    trace(attributeName + " " + author0.attribute(attributeName));
  }

Asyou can see, traversing the XML DOM is effective but laborious. Often,it’s far more effective to use E4X syntax, particularly when youalready know the structure. E4X syntax allows you to access child nodesby name as properties of parent nodes. For example, the followingaccesses the first book node:

  trace(xml.book[0]);

Youcan chain together this simple E4X syntax as in the following example,which retrieves the first author node of the first book node:

  trace(xml.book[0].authors.author[0].toXMLString());

E4X also allows you to easily access attributes using the@ symbol. The following uses this syntax to retrieve the value of the first attribute of the author node:

  trace(xml.book[0].authors.author[0].@first);

Youcan also use E4X filters. Filters are enclosed in parentheses withinwhich you specify conditions. The following example retrieves all theauthor nodes in which the last attribute isKazoun:

  var authors:XMLList = xml.book.authors.author.(@last == "Kazoun");
  for(var i:uint = 0; i < authors.length(); i++) {
    trace(authors[i].parent().parent().toXMLString());
  }

原创粉丝点击