Using jQuery to extract CDATA in XML for use as HTML content
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.
精彩评论