0

I have a requirement to traverse through an XML document and display the nodes that satisfy a specific criteria. I was able to get the file over http and get the nodes that satisfy the criteria from that. But I couldn't present those on the screen nor can I see them.

I tried all different ways but in vain,

XML Structure :

<routeElement>
 <Service>
  <name>RetrieveEmployeeDetailV001</name>
 </Service>
 <Service>
  <name>RetrieveEmployeeAccountDetailV001</name>
 </Service>
</routeElement>

This is the piece of code I am stuck at...

xmlDocument.setProperty("SelectionLanguage", "XPath");
finalData=xmlDocument.selectNodes("//service");

I used different ways to access the child nodes of finalData.

finalData[0].childNodes[0].nodeValue;
finalData.childNodes[0].nodeValue; 
finalData[0].getElementByTagName("service");
finalData[0].firstChild

etc etc. But nothing has worked out.Can you please suggest a solution to the problem ?

1
  • Are you using any javascript frameworks? The link provided in the answer below is for a .NET4.0 component, which is NOT javascript. I wonder if you're trying to do too much of the underlying work yourself, when you could be using something to abstract a lot of the browser inconsistencies. Commented Apr 3, 2011 at 15:48

1 Answer 1

1

If you want to select nodes with javascript/xpath, you can use document.evaluate. Something like:

var xresult = document.evaluate('//service',xmlDocument, null, 
                          XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null),
              result,
              ret = [];
while (result = xresult.iterateNext()) {
   ret.push(result);
};
//=> the `ret`-array should contain the nodes you searched

This will work in most browsers, except IE. For IE use SelectNodes

Alternatively I think regular DOM-methods should work, in this case xmlDocument.getElementsByTagName('service') would return a NodeList

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your comments. I am using IE so I have gone with selectNodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.