Google Map API v3解析XML并通过标记名称获取元素 [英] Google Map API v3 parsing xml and getting elements by tag name

查看:129
本文介绍了Google Map API v3解析XML并通过标记名称获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析我的xml时遇到了一些问题,需要在google map中作为标记重叠,首先,我有一个这样的XML文件

 <根> 

< weather>



< city> city_name< / city>

< level> 1< / level>



< data> weather_data< / data>



< lat> -6.211544< / lat>



< lon> 106.845172< / lon>

 <海拔> 13.41< /仰角> 

< / weather>

 < / root> 

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

解决方案

请参阅geoxml3的源代码( polys分支 kmz branch ); nodeValue函数,这是从kmz分支开始的,poly分支略有不同,或者会起作用:

  / ** 
*提取DOM节点的文本值,修剪前后的空白。
*
* @param {Element}节点XML节点/元素。
* @param {Any} delVal节点不存在时的默认值。
* @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]);
}
}
返回retStr;
}

另一个选项是这是从v2开始的两个Gxml函数的实现。


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?

解决方案

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.

这篇关于Google Map API v3解析XML并通过标记名称获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆