Google Map API V3: Parsing an XML document
I'm trying to update my Google Maps API web application that I had running fine under version two. Version three seems not only to have broken everything but also has placed the burden of writing one's own versions of a lot of missing API functions that are no longer there.
Soooo, what was a relatively simple adaptation of their "store locator" example under version two has turned into a gigantic headache.
More specifically, my problem is parsing the XML document that my PHP/mySQL code returns after the user has entered some data into the web page and sent it off to the server. I know that the test data that I've been entering works because, 1. it worked flawlessly under V2, and 2., If I hardcode it into the PHP page and then load that page I get the expectant XML document loaded in my browser (Firefox 3.6.13 running on Snow Leopard).
Update: After very careful tracing with Firebug I've discovered that "downloadUrl" function from here is returning the data correctly.
However, it looks like the function "GXml.parse(data)" ( from here) isn't processing the returned XML. I'm pasting that code below开发者_开发问答:
function GXml(){}
GXml.value=value;
GXml.parse=parse;
function value(node){
if(!node){
return"";
}
var retStr="";
if(node.nodeType==3||node.nodeType==4||node.nodeType==2){
retStr+=node.nodeValue;
}else if(node.nodeType==1||node.nodeType==9||node.nodeType==11){
for(var i=0;i<node.childNodes.length;++i){
retStr+=arguments.callee(node.childNodes[i]);
}
}
return retStr;
}
function parse(textDoc){
try{
if(typeof ActiveXObject!="undefined"&&typeof GetObject!="undefined"){
var b=new ActiveXObject("Microsoft.XMLDOM");
b.loadXML(textDoc);
return b;
}else if(typeof DOMParser!="undefined"){
return(new DOMParser()).parseFromString(textDoc,"text/xml");
}else{
return Wb(textDoc);
}
}
catch(c){
P.incompatible("xmlparse");
}
try{
return Wb(textDoc);
}
catch(c){
P.incompatible("xmlparse");
return document.createElement("div");
}
}
function P(){}
P.write=function(a,b){}
;P.writeRaw=function(a){}
;P.writeXML=function(a){}
;P.writeURL=function(a){}
;P.dump=function(a){}
;P.incompatible=function(){}
;P.clear=function(){}
;
function Wb(a){
return null;
}
I dont bother parsing XML anymore.. much easier to convert it to JSON and stream it directly into objects.. one example:
Is this the fastest way to parse my XML into JavaScript objects using jQuery?
Duncan.
I'm having the same problems, but I found this after a little bit of search. I haven't followed the tutorial through all the way yet, but I thought I'd go ahead and share it.
http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html
Mia
精彩评论