0

I'm trying to parse an xml file . I'm using the function downloadUrl . The element <link> of my xml file contains a url with ' & ' ( ampersant character ) . The error in my browser says : " Cannot read property 'documentElement' of null " I replace the & with &amp; in a sample file-similar to my xml and the parse runs great!One important information : I cannot edit my xml file . So , the solution is to insert at the javascript code a function to replace this character & with &amp; .Something like that i imagine:

function htmlEscape(items) {
    return String(items)
            .replace(/&/g, '&amp;');
    }

But i cannot find the way on how to do that. Here's is my script :

var infowindow;
      var map;

      function initialize() {
        var myLatlng = new google.maps.LatLng(38.822590,24.653320);
        var myOptions = {
          zoom: 6,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        downloadUrl("moredata.xml", function(doc) {
          var items = doc.documentElement.getElementsByTagName("item");
          for (var i = 0; i < items.length; i++) {
               var description = items[i].getElementsByTagName("description")[0].textContent;
                var temp        = items[i].getElementsByTagName("temp")[0].textContent;
                    var title       = items[i].getElementsByTagName("title")[0].textContent;
            var link        = items[i].getElementsByTagName("link")[0].textContent;
            var latlng      = new google.maps.LatLng(parseFloat(items[i].getElementsByTagName("glat")[0].textContent),
                                                     parseFloat(items[i].getElementsByTagName("glon")[0].textContent));

a part of my xml :

<channel>
    <title>
        Real time weather in Greece
    </title>
    <link>http://www.123.gr/</link>
    <image>
    <url>
    http://www.123.gr/templates/metar/images/metar.gif
    </url>
    <title>123.gr</title>
    <link>http://www.123.gr/</link>
    </image>
    <description>
    Real time weather in Greece
    </description>
    <language>el</language>
    <pubDate>Thu, 03 Apr 2014 17:08:10 +0300</pubDate>
    <copyright>123.gr</copyright>
    <managingEditor>kondilis@123.gr</managingEditor>
    <webMaster>admin@123.gr</webMaster>
    <item>
    <title>Center</title>
    <description>Salonica</description>
    <link>
        http://www.metar.gr/index.php?option=com_jumi&fileid=12&Itemid=73&station=1227
    </link>
    <temp>16.7 °C</temp>
    <glat>40.422726139672626</glat>
    <glon>22.93392777442932</glon>
    </item>
</channel>

the downloadUrl function :

function downloadUrl(url, callback) {
 var status = -1;
 var request = createXmlHttpRequest();
 if (!request) {
   return false;
 }

 request.onreadystatechange = function() {
   if (request.readyState == 4) {
     try {
       status = request.status;
     } catch (e) {
       // Usually indicates request timed out in FF.
     }
     if (status == 200) {
       callback(request.responseXML, request.status);
       request.onreadystatechange = function() {};
     }
   }
 }
 request.open('GET', url, true);
 try {
   request.send(null);
 } catch (e) {
   changeStatus(e);
 }
};
marYOs
  • 17
  • 5

1 Answers1

0

The problem is that responseXML is broken because of an invalid XML document, it's not just an invalid string. You will probably have to retrieve the responseText, replace the ampersands with properly encoded entities and then do something like the solutions to this SO question to make that string into a traversable XML document

Community
  • 1
  • 1
Rob M.
  • 35,491
  • 6
  • 51
  • 50