Check for xml attribute in as3

What is the best method for checking attribute existence for an XML object in ActionScript 3?

http://martijnvanbeek.net/weblog/40/testing_the_existance_of_an_attribute_in_xml_with_as3.html suggests testing with

   if ( node.@test != node.@nonexistingattribute )

and I saw comments suggesting using:

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

But in both cases, the tests are case sensitive.

From the XML Specificity : “XML processors must match case-insensitive symbol names”, so I assume that the attribute name should also be a case-insensitive comparison.

thank

+3
source share
2 answers

Please read your quote carefully from the XML specs:

XML

4.3.3 , , , encoding <?xml>, "UTF-8" "UTF-8". / .

2.3 Common Syntactic Constructs, . , .

, Flash:

for each ( var attr:XML in xml.@*) {
   if (attr.name().toString().toLowerCase() == test.toLowerCase()) // attribute present if true
}

:

var found:Boolean = false;
for each ( var attr:XML in xml.@*) {
    if (attr.name().toString().toLowerCase() == test.toLowerCase()) {
        found = true;
        break;
    }
}
if (found) // attribute present
else // attribute not present
+9

XML () XMLList length() ?

.

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

trace(xml.children().@id.length());//test if any children have the id attribute
trace(xml.child[1].@id.length());//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()
0

Source: https://habr.com/ru/post/1785115/


All Articles