验证和获取此XML属性的更好方法是什么?

时间:2011-08-26 02:00:29

标签: xml actionscript-3

我正在使用Actionscript 3中的XML E4X,并且正在查看Senocular关于过滤器http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4的文章,以便更好地验证下面的代码?

本质上,我想检查配置文件节点是否存在,以及是否具有匹配的locale属性的配置文件节点(通过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>

1 个答案:

答案 0 :(得分:0)

你可以做点什么:

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

我还建议将实际节点文本包装在CDATA标签中,这样内容就不会被解析为XML标记,你可以包含像“&lt;”这样的字符。和“&amp;”在你的文字中。

IE

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