我正处在一个巨大的困境中,无论如何也想不出我到底做错了什么我已经为其他项目编写了几个其他代码,它们做了完全相同的事情-输出显示XML文件数据的表,但对于这个项目,它只是不起作用!
下面是我的代码:<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>
Internet explorer部分(for循环到末尾)工作完美。我理解IE代码不依赖于名称空间(s)(URL),但名称空间和名称空间URL在我从中提取它们的XML文件中是完美的。路径是完美的,因为它在IE中工作。在Firefox中运行它并观察开发人员控制台,如果我们尝试打印aname2,那么document.write(aname2. childnodes [0].nodeValue);它报告aname2 = null…
任何帮助将非常感激!我来自澳大利亚,我会很高兴地喊你喝一杯或ps3什么的=)
有趣,我发现你的问题,因为我在Internet Explorer遇到麻烦=)
我一直在其他浏览器中这样做:
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;
};
在本例中,我用节点填充数组,但您可以做任何您喜欢的事情。