Get Flex XML attribute name

Theattributes() method returns anXMLList object with all the data from the attributes contained within anXML object. You can call thename() method for each attribute in theXMLList to 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 theXML object 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));
  }

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

  trace(xml.book[0]);

You can 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);

You can also use E4X filters. Filters are enclosed in parentheses within which you specify conditions. The following example retrieves all the author 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());
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值