Can't output xPath data using Javascript in Firefox
I'm suffering from a huge dilemna and cannot for the life of me, work out what I have done wrong :S I have written several other code for other projects which do exactly the same thing - Output a table displaying data from an XML file, but for this project it just does not work!
Here is my code:
<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
xml=loadXMLDoc("AH_vic.xml");
var aname="/AirportHeliport/timeSlice/AirportHeliportTimeSlice/name";
var acoord="/AirportHeliport/timeSlice/AirportHeliportTimeSlice/ARP/ElevatedPoint/gml:coordinates";
if (typeof xml.evaluate !== 'undefined')
{
var result = xml.evaluate(
aname,
xml,
function (prefix) {
if (prefix === 'gml') {
return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null
);
var result2 = xml.evaluate(
acoord,
xml,
function (prefix) {
if (prefix === 'gml') {
return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null
);
// now use the code here you already have in your sample for evaluate
var nodes=xml.evaluate(
aname,
xml,
function (prefix) {
if (prefix === 'gml') {
return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null);
var nodes2=xml.evaluate(
acoord,
xml,
function (prefix) {
if (prefix === 'gml') {
开发者_开发技巧 return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null);
var aname2=nodes.iterateNext();
var acoord2=nodes2.iterateNext();
//document.write(aname2.childNodes[0].nodeValue());
document.write("<table border=2><tr><td><b>Name</b></td><td><b>Coordinates</b></td></tr>");
while (aname2)
{
document.write("<tr><td>");
document.write(aname2.childNodes[0].nodeValue);
document.write("<br /><td>");
document.write(acoord2.childNodes[0].nodeValue);
document.write("<br /><td>");
aname2=nodes.iterateNext();
acoord2=nodes2.iterateNext();
}
document.write("</td></tr></table>");
}
else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined')
{
//xml.setProperty('SelectionLanguage', 'XPath');
//xml.setProperty('SelectionNamespaces', 'xmlns:gml="http://www.opengis.net/gml/3.2"');
var nodes = xml.selectNodes(aname);
var nodes2 = xml.selectNodes(acoord);
// now use the code you already have for selectNodes
document.write("<table border=2><tr><td><b>Name</b></td><td><b>Coordinates</b></td></tr>");
for (i=0;i<nodes.length;i++)
{
document.write("<tr><td>");
document.write(nodes[i].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(nodes2[i].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
}
</script>
</body>
</html>
The Internet explorer part (for loop towards the end) works flawlessly. I understand that IE code is not dependant on namespace(s)(URL's), but the namespace and namespace URL is perfect in the XML file from which I am extracting them from. Path is perfect as it works in IE. Running it in Firefox and observing the developer console, if we try to print out aname2, so document.write(aname2.childNodes[0].nodeValue); It reports that aname2 = null...
Any help would be much appreciated! I am from Australia and will gladly shout you's a drink or a ps3 or something =)
Funny, I found your question because I'm having trouble on Internet Explorer =)
I've been doing it in the other browsers like this:
namespace.prototype.xpath = function(xml, path) {
var array = [];
var appendFromNodes = function(object, nodes) {
for (var index in nodes) {
var childNode = nodes[index];
if (childNode.nodeName) {
object[childNode.nodeName] = childNode.textContent;
}
}
};
if (xml.evaluate) { // checks if browser supports the W3C way of doing it (no IE)
var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
while (result) {
var tmp = {};
appendFromNodes(tmp, result.childNodes);
for (var index in result.attributes) {
var attribute = result.attributes[index];
if (attribute.nodeName) {
tmp[attribute.nodeName] = attribute.nodeValue;
}
}
array.push(tmp);
result = nodes.iterateNext();
}
}
return array;
};
In this case, I'm filling an array with the nodes, but you can do whatever you please.
精彩评论