2011-08-26 59 views
0

我得到我的身邊XML E4X頭在ActionScript 3和一直在尋找Senocular的有關驗證下面的代碼的一種更好的方式過濾器http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4文章?更好的辦法?

基本上我想檢查節點存在的輪廓,如果那裏有一個匹配的語言環境屬性的輪廓節點(通過FlashVars傳遞),如果不是搶第一點的文本。反正繼承人的代碼段,我的XML如下:

function addInfoBubble(countryName:String, countryDataXML:XML):void { 

// (theres other non related code here) 

    if(countryDataXML.achievers.achiever[0].profile as XMLList && countryDataXML.achievers.achiever[0].profile.length() > 0){ 
       var localeStr:String = countryDataXML.achievers.achiever[0].profile.attribute("locale") as String; 
       if(!localeStr){ 
        descStr = countryDataXML.achievers.achiever[0].profile[0].text(); 
        if(!descStr){ 
         descStr = "(No profile available)"; 
        } 
       } 
      } else { 
       descStr = "(No profile available)"; 
      } 

我的XML

<?xml version="1.0" encoding="utf-8"?> 
<countries> 
    <country name="China"> 
    <achievers> 
     <achiever> 

     <photoURL>images/jerry-chen.jpg</photoURL> 
     <name>Jermy Chan</name> 
     <profile locale="en_US"> 
      ZZLorem ipsum dolor sit amet, consectetur adipiscing elit. Donec suscipit, diam in venenatis lacinia, enim libero luctus nulla, gravida bibendum. 
     </profile> 
     <profile locale="zh_CN"> 
      Chinese translation 
     </profile> 

     </achiever> 
    </achievers> 
    </country> 

回答

0

,你可以這樣做:

for each (var profileNode:XML in countriesXML.profile){ 
    if ([email protected]() == ''){ 
     descStr = 'No profile available'; 
    }else{ 
     descStr = profileNode; 
    } 
} 

我也建議您包裹的實際節點文本在CDATA標籤,這樣的內容不會被解析爲XML的標記,你可以加入你的文本「<」和「&」字。

IE

<profile><![CDATA[SOME AWESOME TEXT & STUFF in here <>]]></profile>