I'm trying to select a specific HTML element in a document, for firefox i just use:
xpathobj = document.evaluate(xpath, document, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null);
which works fine. However when I try the IE equivilent:
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(document);
xmlDoc.setProperty("SelectionLanguage", "XPath");
xpathobj = xmlDoc.selectNodes(xpath);
I get no object returned. So my question is there an easy way to use XPath to get to the element I want in IE? The XPath I'm using looks like
/HTML/BODY/DIV[9]/DIV[2]
There are some bugs in oly1234's code I try to fix it as follows:
Are you sure X-Path is implemented in your version of Internet Explorer? As in: what version are you using?
Take a look at http://dev.abiss.gr/sarissa/ project. They have migrated most of XML-related APIs to IE. Otherwise it is indeed also easy to implement. The problems you would need to solve would be: serialization of HTML into valid XML, syncing result of XMLDOM XPath query with original HTMLDOM. To my knowledge they've done it in their library, however, its performance could have been better.
Instead of doing
try
This would only really work if your your HTML document is formatted to XHTML standards. Also, the BODY tag would be the root node, so you would have to change your XPATH to "/BODY/DIV[9]/DIV[2]"
Hi well in the end i came up with my own dodgy solution, any sugestions on improving it would be greatly apreciated. It makes use of some prototype functionality:
Works in IE5+ with xpath of the form "/HTML/BODY/DIV[9]/DIV[2]"
function getXPathElement (xpath , element) {
}
function getXPathElementByIndex(decendents, xpathSegment){
}
Thanks to everyone for thier help, either way i learnt a fair bit about various javascript frameworks.
I can not find a simple and common solution, you can write custom function to implement a little of xpath but it is hard to get complete in internet explorer 6 or lower version....