Reputation: 413
I Have a web applicaton and now I want to improve it so it works in FireFox. My problem is how to use XPath in a cross-browser manner.
I have this script:
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = "false";
oXmlDoc.loadXML(document.getElementById(MasterObj + "txtMenu").value);
var xmlNodes = oXmlDoc.documentElement.selectNodes("/FormGeneratorEntity/GetMenu[TemplateID=" + obj.value + "]");
var len = xmlNodes.length
for (var i = 0; i < len; i++) {
alert(xmlNodes.item(i).selectSingleNode('Title').text)
}
And my xml string looks like this:
<FormGeneratorEntity><GetMenu><Title>MyTitle</Title><Val>MyValue<Val/></GetMenu></FormGeneratorEntity>
How can I make this work in Firefox?
Upvotes: 0
Views: 1086
Reputation: 348992
Your question is not "How to use XPath in Firefox", but "How to create a XML document from a string (in Firefox)".
The answer to that question is the DOMParser
, more specifically the parseFromString
method:
oXmlDoc = new DOMParser().parseFromString(content_string, "text/xml");
Implemented in your current code:
var oXmlDoc,
content_string = document.getElementById(MasterObj + "txtMenu").value;
if (window.DOMParser) {
oXmlDoc = new DOMParser().parseFromString(content_string, "text/xml");
} else if (window.ActiveXObject) {
oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = "false";
oXmlDoc.loadXML(content_string);
} else {
// Not supported. Do something
}
If you're still looking for excellent documentation on XPath, have a look at the Mozilla Developer Network: XPath.
Upvotes: 0
Reputation: 7215
Can you use jQuery? If so, it would be a much more browser-friendly solution.
Upvotes: 1