开发者

AS3: XPath returns XMLNode instead of XMLList if single match?

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?

Th开发者_JAVA百科anks 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]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜