Flash / Flex / ActionScript/XML/Equality
Содержание
- 1 A comparison between an XML instance and an XMLList with only one XML instance is treated as a direct comparison between the two XML instances
- 2 An element is equal to an XML attribute if the element contains no child and the text contained by the element matches the attribute value.
- 3 An element is equal to an XML instance representing a text node if the element contains no child elements, and the text it contains matches the text node"s value.
- 4 By default, E4X ignores whitespace nodes when comparing XML for Equality
- 5 Compare two xml variable
- 6 To not to ignore whitespace nodes prior to parsing
- 7 XML Equality
A comparison between an XML instance and an XMLList with only one XML instance is treated as a direct comparison between the two XML instances
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var msg1:XML = <GREETING> <TO>A</TO> <FROM>Programmer</FROM> <MESSAGE>Hello</MESSAGE> </GREETING>; var msg2:XML = <GREETING> <TO>A</TO> <FROM>Programmer</FROM> <MESSAGE>Hello</MESSAGE> </GREETING>; trace(msg1.FROM == msg2.*[1]); //
} }
}
</source>
An element is equal to an XML attribute if the element contains no child and the text contained by the element matches the attribute value.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var product:XML = <PRODUCT QUANTITY="1"><COST>1</COST></PRODUCT>; trace(product.@QUANTITY == product.COST); // Displays: true
} }
}
</source>
An element is equal to an XML instance representing a text node if the element contains no child elements, and the text it contains matches the text node"s value.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var product:XML = <PRODUCT> <COST>1</COST> <QUANTITY>1</QUANTITY> </PRODUCT>; trace(product.COST.*[0] == product.QUANTITY); // Displays: true } }
}
</source>
By default, E4X ignores whitespace nodes when comparing XML for Equality
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var x1:XML = <a></a>; var x2:XML = <a> </a>; trace(x1 == x2); // Still displays: true
} }
}
</source>
Compare two xml variable
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var msg1:XML = <GREETING> <TO>A</TO> <FROM>Programmer</FROM> <MESSAGE>Hello</MESSAGE> </GREETING>; var msg2:XML = <GREETING> <TO>A</TO> <FROM>Programmer</FROM> <MESSAGE>Hello</MESSAGE> </GREETING>; trace(msg1.* == msg2.*); // Displays: true
} }
}
</source>
To not to ignore whitespace nodes prior to parsing
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
XML.ignoreWhitespace = false; // Don"t ignore whitespace nodes var x1:XML = <a></a>; var x2:XML = <a> </a>; trace(x1 == x2); // Now displays: false } }
}
</source>
XML Equality
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var x1:XML = <a></a>; var x2:XML = <a></a>; trace(x1 == x2); // Displays: true
} }
}
</source>