AS3

I'm trying to dynamically load pictures based on XML file. The problem that Im stuck with is a function that is supposed to return found node from supplied identifier. Like in the following code

function getStudyById(id:String):XMLNode{
    var xl:XMLList = xml.bacase.(@name==id); // xml is a global variable
    return xl.children()[0]; // xl[0] does not work either
    // return (xl.length()>0) ? xl[0] : null;
}

The XML goes like this:

<root>
    <bacase name="a" />
    <bacase name="b" />
</root>

Anticipating your questions, it all is done in the COMPLETE event of the xml loader, once the XML has been loaded.

Now the problem is that the XMLList appears to be empty, while xml is correctly populated and the identifier definitely matches one of the nodes. When I duplicate the queried node, the list traces correctly as containing two nodes. So my guess is that, if there is only a single match, the XMLList does not get created, and the result contains data of XMLNode data type.

Anyone there to confirm this? If so, is there a way to ensure that the query always returns XMLList type?

Thanks Artur

有帮助吗?

解决方案

XMLNode works only with the old flash.xml.XMLDocument. A single node would be of type XML if you use XML or XMLList.

This snipped works fine for me:

var xml:XML = <root>
        <bacase name="a" />
        <bacase name="b" />
    </root>;

var p:XMLList = xml.bacase.(@name == "a");

if (p.length() > 0) return XML(p[0]);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top