Google Map API v3 parsing xml and getting elements by tag name

坚强是说给别人听的谎言 提交于 2019-12-12 02:45:03

问题


I'm having some problems in parsing my xml to be overlayed in google map as markers, first off, I have an XML file like this

<root>

<weather>

<city>city_name</city>

<level>1</level>

<data>weather_data</data>

<lat>-6.211544</lat>

<lon>106.845172</lon>

<elevation>13.41</elevation>

</weather>

</root>

as you can see I contain the necessary data inside one tag, not attribute, problem is, in V2 I used GDownloadUrl and GXml parse and getelementsbytagname method works. In V3 it seems I can't use GDownloadUrl, and all the examples I've seen on the internet uses attribute parsing. Can anyone please help?


回答1:


See the source for geoxml3 (either the polys branch or the kmz branch); the nodeValue function, this is from the kmz branch, the polys branch is slightly different, either will work:

/**
 * Extract the text value of a DOM node, with leading and trailing whitespace trimmed.
 *
 * @param {Element} node XML node/element.
 * @param {Any} delVal Default value if the node doesn't exist.
 * @return {String|Null}
 */
geoXML3.nodeValue = function(node, defVal) {
  var retStr="";
  if (!node) {
    return (typeof defVal === 'undefined' || defVal === null) ? null : defVal;
  }
  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;
}

Another option is this implementation of the two Gxml functions from v2.



来源:https://stackoverflow.com/questions/13657364/google-map-api-v3-parsing-xml-and-getting-elements-by-tag-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!