开发者

Display nodes in JavaScript

I have a requirement to traverse through an XML do开发者_如何学JAVAcument 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 ?


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜