Google Map API v3解析xml并按标记名称获取元素

时间:2012-12-01 07:23:10

标签: google-maps-api-3 xml-parsing

我在解析我的xml时遇到了一些问题,要在谷歌地图中作为标记覆盖,首先,我有一个像这样的XML文件

<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>

你可以看到我在一个标签内包含必要的数据,而不是属性,问题是,在V2中我使用了GDownloadUrl和GXml parse以及getelementsbytagname方法。在V3中似乎我不能使用GDownloadUrl,我在互联网上看到的所有示例都使用属性解析。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

查看geoxml3的来源(polys branchkmz branch); nodeValue函数,这是来自kmz分支,聚合分支略有不同,要么都可以工作:

/**
 * 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;
}

另一个选择是this从v2实现两个Gxml函数。

相关问题