2010-07-11 244 views
1

試圖解析一些XML(在這我管不着!)選擇XML節點

在C#中我會做這樣的事情:

XmlNodeList xnList = xml.SelectNodes("/Names/Name[@type='M']"); 

可這在AS3做?

回答

26

簡短的回答,是的:

var xnList:XMLList = xml.Names.Name.(@type == "M"); 

更長的版本:

var xml:XML = <Root> 
     <Names> 
      <Name type="M" value="John Doe" /> 
      <Name type="F" value="Jane Doe" /> 
      <Name type="M" value="John Hancock" /> 
     </Names> 
     <Other /> 
     </Root> 

var xnList:XMLList = xml.Names.Name.(@type == "M"); 

//test 
for each (var xnNode:XML in xnList) trace(xnNode.toXMLString()) 

有一個相當不錯的E4X tutorial on the Yahoo Developer Network

HTH

+0

+1好答案。 – gMale 2010-07-12 01:34:08

+0

+1的答案 – o15a3d4l11s2 2010-07-12 08:56:01

+0

你可以做通配符嗎? // * – FlavorScape 2012-08-20 19:54:23