开发者

Better way of validating and grabbing this XML attribute?

开发者 https://www.devze.com 2023-03-30 17:06 出处:网络
I\'m getting my head around XML E4X in Actionscript 3 and been looking at Senocular\'s article about filters http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4 for a better way of valida

I'm getting my head around XML E4X in Actionscript 3 and been looking at Senocular's article about filters http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4 for a better way of validating the code below?

Essentially I want to check the profile nodes exist and if theres a profile node with a matching locale attribute (passed via FlashVars), and if not it grab the text of the first node. Anyway heres the code snippet and my XML is below:

 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;
             开发者_如何学Python   if(!localeStr){
                    descStr = countryDataXML.achievers.achiever[0].profile[0].text();
                    if(!descStr){
                        descStr = "(No profile available)";
                    }
                } 
            } else {
                descStr = "(No profile available)";
            }

My 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>


you could do something like:

for each (var profileNode:XML in countriesXML.profile){
    if (profileNode.@locale.toString () == ''){
        descStr = 'No profile available';
    }else{ 
        descStr = profileNode; 
    }
}

I'd also recommend wrapping your actual node text in CDATA tags, so the content will not be parsed as XML markup and you can include characters like "<" and "&" in your text.

IE

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

精彩评论

暂无评论...
验证码 换一张
取 消