Using jQuery to extract CDATA in XML for use as HT

2019-07-16 13:06发布

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)
  });
});

1条回答
2楼-- · 2019-07-16 13:23

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.

查看更多
登录 后发表回答