My apologies in advance if this is not the forum for this question.
I'll admit up front that JavaScript has never been a strong suit for me and
that parsing XML in any language is still new to me. After looking at a few
different parsers out there, it looks like trying the XPath route might be
my best bet as the data I want to retrieve will be coming from a consistent
source and I will always be looking for the same tags/nodes/elements
(whatever the correct terminology might be.
All that said, I seem to have hit quite a stumbling block in attempting to
utilize XPath... I've looked at a few pages with these two being most
useful:
*
http://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript
* http://www.zvon.org/xxl/XPathTutorial/General/examples.html
But I can't seem to get my test code to return any meaningful results. So,
without further ado, my sample data and code....
I am using data I found at
http://www-128.ibm.com/developerworks/xml/library/x-ffox3/ (labels.xml) as
my sample. I can get the data loaded into an XMLDocument and work with it in
the usual ways, but none of the parsers I could find would allow me to drill
down to a specific element by name in a way that worked for my application.
So, now some sample JS code... If I do this:
var nsResolver = XMLData.createNSResolver( XMLData.ownerDocument == null ?
XMLData.documentElement : XMLData.ownerDocument.documentElement);
var LabelCount = XMLData.evaluate("count(/labels/label)", XMLData,
nsResolver, XPathResult.NUMBER_TYPE, null ).numberValue;
LabelCount is now equal to 4 as expected. However, I seem to be unable to
access a particular /labels/label/name to get a value... Here's my (latest)
code:
var Name = XMLData.evaluate("/labels/label[1]/name", XMLData, nsResolver,
XPathResult.STRING_TYPE, null );
In this example, Name.textContent is 'undefined' when I alert() it. I even
tried the following to see what would come back...
var Name = XMLData.evaluate("//label/name", XMLData, nsResolver,
XPathResult.ANY_TYPE, null );
alert(Name.resultType); //displays: 4
alert(Name.textContent); //displays: undefined
Name.iterateNext;
alert(Name.textContent); //displays: undefined
So! Am I way off in my implementation? I assume that I must be to achieve
the results that I am...
Thanks.
Dan
|