Skip to main content
Participating Frequently
May 30, 2007
Answered

XPathAPI

  • May 30, 2007
  • 6 replies
  • 465 views
Hi all,

I have been unable to get the XPathAPI to work. Here is my code:

var layerList:Array = XPathAPI.selectNodeList(this.firstChild, "/ViewContext/LayerList/Layer");

//grab the name
for(i = layerList.length - 1; i >= 0; i--)
{
var xmlNode:XMLNode = layerList ;
var lName:String = XPathAPI.selectSingleNode(xmlNode.firstChild, "Name").firstChild.nodeValue;
}

The var lName is returned as undefined. Any ideas?

Thank you in advance!
This topic has been closed for replies.
Correct answer Greg Dove
Ah...try this:

var lName:String = XPathAPI.selectSingleNode(xmlNode, "Layer/Name").firstChild.nodeValue;

6 replies

Inspiring
May 30, 2007
No problems
SemanticaAuthor
Participating Frequently
May 30, 2007
That worked! Thank you GWD!
Greg DoveCorrect answer
Inspiring
May 30, 2007
Ah...try this:

var lName:String = XPathAPI.selectSingleNode(xmlNode, "Layer/Name").firstChild.nodeValue;
Inspiring
May 30, 2007
Just a quick check... do you have the import statement

import mx.xpath.XPathAPI;

as I mentioned earlier?
SemanticaAuthor
Participating Frequently
May 30, 2007
Yes I do, also I am using Flash 8 Professional if that makes a difference.
Inspiring
May 30, 2007
Oh... and make sure you have this somewhere.. .otherwise it will definitely return undefined

import mx.xpath.XPathAPI;
Inspiring
May 30, 2007
Its helpful to post a bit of your xml as well. And its best to use the attach code button for posting code... as you can see it messes with the formatting. I answered a question yesterday where someone had used an uppercase i in the array access and lowercase i in the loop construct... and it wasn't visible.

First check your layerList has the correct nodes with a trace (it should output xml nodes separated by commas.
...
var layerList:Array = XPathAPI.selectNodeList(this.firstChild, "/ViewContext/LayerList/Layer");
trace(layerList);
//grab the name
...

then I suggest you repost the code and attach a little of your xml


Is your xml like this?

Is your xml structure as follows?

<ViewContext>
<LayerList>
<Layer>
<Name>MyName</Name>
</Layer>
<Layer>
<Name>MyName</Name>
</Layer>
<Layer>
<Name>MyName</Name>
</Layer>
</LayerList>
</ViewContext>
SemanticaAuthor
Participating Frequently
May 30, 2007
Thanks GWD!

Attached is my xml and code as requested.