Using E4X XML comparison results in loss of XML default namespace on Android
Just a heads up on an obscure bug I found when using XML on android.
When working with XML using a set default namespace, if you generate a number of XMLLists using a comparision operator, the default namespace is lost. This only occurs when running on Android devices - other platforms and the Air simulator is fine.
So if you take some XML like this...
private var testXML:XML = <documentRoot xmlns="www.mydomain.com">
<subNode id="1">A</subNode>
<subNode id="2">B</subNode>
<subNode id="3">C</subNode>
<subNode id="4">D</subNode>
<subNode id="5">E</subNode>
<subNode id="6">F</subNode>
<subNode id="7">G</subNode>
<subNode id="8">H</subNode>
</documentRoot>;
.. and run a function like this...
private function xmlTest():void
{
//Our target ID.
var target:uint = 4;
//Set default namespace
default xml namespace = NS;
trace("TEST START");
trace("HOW MANY SUBNODES: " + (this.testXML.subNode).length());
trace("HOW MANY SUBNODES WITH ID = 4 : " + (this.testXML.subNode.(@id == target)).length());
trace("HOW MANY SUBNODES WITH ID = 4 : " + (this.testXML.subNode.(@id == target)).length());
trace("HOW MANY SUBNODES WITH ID = 4 : " + (this.testXML.subNode.(@id == target)).length());
trace("HOW MANY SUBNODES WITH ID = 4 : " + (this.testXML.subNode.(@id == target)).length());
trace("HOW MANY SUBNODES WITH ID = 4 : " + (this.testXML.subNode.(@id == target)).length());
}
You get this....
TEST START
HOW MANY SUBNODES: 8
HOW MANY SUBNODES WITH ID = 4 : 1
HOW MANY SUBNODES WITH ID = 4 : 1
HOW MANY SUBNODES WITH ID = 4 : 0
HOW MANY SUBNODES WITH ID = 4 : 0
HOW MANY SUBNODES WITH ID = 4 : 0
From the tests I've done - whats happening is that the use of the (@id == target) is causing the loss of the default namespace after the second call. You either have to reset the default namespace with another : default xml namespace = NS; ... or ... fully declare the namespace each time eg. (this.testXML.NS::subNode.(@id == target)).length());
Bug has been logged (Bug 3615764)
