使用jQuery提取XML中的CDATA以用作HTML内容 [英] Using jQuery to extract CDATA in XML for use as HTML content

查看:258
本文介绍了使用jQuery提取XML中的CDATA以用作HTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检索Google地球.kml(xml)文件并使用这些内容在Google地图中放置标记。
我感兴趣的特定XML标记如下所示:

 < Placemark> 
< name> Bahamas-LSI< / name>
< description><![CDATA [
< img src =http://coralreefwatch.noaa.gov/satellite/ge/data/current/vs_Bahamas_LSI.bmp>
< p>
- < a href =http://coralreefwatch.noaa.gov/satellite/virtual_stations/greater_caribbean.html#Bahamas_LSI>
SST / DHW时间系列< / a> ;.
< p>
- 通过电子邮件coralreefwatch@noaa.gov为本网站免费订阅自动电子邮件漂白警报。
]]>< / description>
< Snippet>< / Snippet>
< LookAt>
<经度> -76.5000< /经度>
< latitude> 23.5000< / latitude>
..
< /地标>

以下代码提取了名称和lat&长,但是我不知道如何使用jQuery从描述标签中提取CDATA。我希望能够提取实际的html,以便我可以在Google Maps标记的infowindow中使用它。

  // 
jQuery.get(CRWGE_current_products.kml,{},function(data){
//对于每个地标标记,提取名称和latlong
//然后绘图
jQuery(data).find(placemark)。each(function(){
var station = jQuery(this);
var name = station.find('name')。 text();
var latlng = new google.maps.LatLng(parseFloat(station.find('latitude')。text()),
parseFloat(station.find('longitude')。text ());

//为特定于站点的数据获取html
var content = station.find('description')。text();
console.log内容);< ---空字符串

setMarker(map,latlng,name,stressIcon,content)
});
});


解决方案

以xml格式检索它,您应该能够调用文本正确:
$ b

jQuery.get(CRWGE_current_products.kml,{},function(data){},'xml'); / code>



我已经使用USGS.gov的xml提要完成了这项工作,但我没有用.kml尝试过。

I am retrieving a Google Earth .kml (xml) file and using the content to place markers in Google Maps. The particular XML tags I am interested in look like:

<Placemark>
       <name>Bahamas-LSI</name>
       <description><![CDATA[
       <img src="http://coralreefwatch.noaa.gov/satellite/ge/data/current/vs_Bahamas_LSI.bmp">
       <p>
       - <a href="http://coralreefwatch.noaa.gov/satellite/virtual_stations/greater_caribbean.html#Bahamas_LSI">
         SST/DHW time series</a>.
       <p>
       - E-mail coralreefwatch@noaa.gov to subscribe free<br>automatic e-mail bleaching alert for this site.
       ]]></description>
       <Snippet></Snippet>
       <LookAt>
       <longitude>-76.5000</longitude>
       <latitude>23.5000</latitude>
       ..
</Placemark>

The following code extracts the name and lat & long, however I don't know how to extract the CDATA from the description tag using jQuery. I would like to be able to extract the actual html so I can then use it in an infowindow for the Google Maps marker.

// 
jQuery.get("CRWGE_current_products.kml", {}, function(data) {
  // for each placemark tag, extract the name & latlong
  // and then plot
  jQuery(data).find("placemark").each(function() {
    var station = jQuery(this);
    var name = station.find('name').text();
    var latlng = new google.maps.LatLng(parseFloat(station.find('latitude').text()),
                                      parseFloat(station.find('longitude').text()));

    // get html for station-specific data
    var content = station.find('description').text();
    console.log(content); <--- empty string

    setMarker(map, latlng, name, stressIcon, content)
  });
});

解决方案

Retrieve it as xml and you should be able to call text properly:

jQuery.get("CRWGE_current_products.kml", {}, function(data) { }, 'xml');

I've done this with xml feeds from USGS.gov, but I haven't tried it with .kml.

这篇关于使用jQuery提取XML中的CDATA以用作HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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