2011-01-13 16 views
3

在ActionScript 3中测试XML对象上属性的存在的最佳方法是什么?在as3中测试xml属性的存在

http://martijnvanbeek.net/weblog/40/testing_the_existance_of_an_attribute_in_xml_with_as3.html是建议使用

if ([email protected] != [email protected]istingattribute) 

测试,我看到了意见建议使用:

if (node.hasOwnProperty('@test')) { // attribute qtest exists } 

但在这两种情况下,测试是区分大小写的。

XML Specs:“XML处理器应该以不区分大小写的方式匹配字符编码名称”,所以我认为属性名称也应该使用不区分大小写的比较匹配。

谢谢

+0

吁...闪光只是不停止奇怪我复活节彩蛋... – jayarjo 2011-01-13 09:07:14

+0

尽管这不是一个解决方案,但我会将xml作为字符串,小写它,再次导入为xml,并安全地使用区分大小写的搜索。 – jayarjo 2011-01-13 09:14:06

+0

@jayarjo:...它会将XML中的所有字符数据内容小写 - 不是一种安全的方式来处理除了不区分大小写的属性名称搜索以外的任何其他用途的XML。 – weltraumpirat 2011-01-13 10:06:56

回答

9

请从XML规范重新仔细阅读您的报价:

XML处理器应该在不区分大小写的 方式

这是章匹配字符 编码名称4.3.3中描述的character encoding declarations的规格,并且它指的是只有的名称存在于encoding的值中<?xml>处理指令,例如"UTF-8""utf-8"。我完全没有理由将它应用于文档中任何位置的属性名称和/或元素名称。

事实上,在规范的第2.3节(Common Syntactic Constructs)中没有提及此处,其中指定了名称和名称标记。对特殊字符等有一些限制,但对大小写字母绝对没有限制。

要做比较不区分大小写的,你必须做在Flash:

for each (var attr:XML in [email protected]*) { 
    if (attr.name().toString().toLowerCase() == test.toLowerCase()) // attribute present if true 
} 

或者说:

var found:Boolean = false; 
for each (var attr:XML in [email protected]*) { 
    if (attr.name().toString().toLowerCase() == test.toLowerCase()) { 
     found = true; 
     break; 
    } 
} 
if (found) // attribute present 
else // attribute not present 
0

如何使用XML的contains()方法或XMLList的length()方法?

例如

var xml:XML = <root><child id="0" /><child /></root>; 

trace(xml.children()[email protected]());//test if any children have the id attribute 
trace(xml.child[1][email protected]());//test if the second node has the id attribute 
trace(xml.contains(<nephew />));//test for inexistend node using contains() 
trace(xml.children().nephew.length());//test for inexistend node using legth()