0

I'm having a problem and it's similar to this question :
Can't get element javascript google apps script

Well , i'm parsing an xml file with the DOM method.I didn't have any problem until i push in my xml the element <link>. When i put a randomized link i have no problem.But if i insert one of my real links of my xml file something goes wrong.

Ι collocate you one of the real links : <link>http://www.metar.gr/index.php?option=com_jumi&fileid=12&Itemid=73&station=1483</link>.

Any ideas what is going wrong?

Here's is my code :

downloadUrl("moredata.xml", function(data) {
      var items = data.documentElement.getElementsByTagName("item");
      for (var i = 0; i < items.length; i++) {
        var description = items[i].getElementsByTagName("description")[0].innerHTML;
        var temp        = items[i].getElementsByTagName("temp")[0].innerHTML;
        var title       = items[i].getElementsByTagName("title")[0].innerHTML;
        var windSpeed   = items[i].getElementsByTagName("windSpeed")[0].innerHTML;
        var dailyRain   = items[i].getElementsByTagName("dailyRain")[0].innerHTML;
        var latlng = new google.maps.LatLng(parseFloat(items[i].getElementsByTagName("glat")[0].innerHTML),
                                    parseFloat(items[i].getElementsByTagName("glon")[0].innerHTML));
       }
})

and the moredata.xml file :

<item>
      <description>Giannena</description>
      <glat>39.62209843837158</glat>
      <glon>20.89027225971222</glon>
      <title>ipiros</title>
      <temp>-16.9°C</temp>
      <dailyRain>0.0 mm</dailyRain>
      <windSpeed>10 km/hr</windSpeed>
      <fire>2</fire>
      <outsideHumidity>88 %</outsideHumidity>
      <link>
        http://this_is_the_link.com
 </link>
  </item>
  <item>
      <description>Athina</description>
      <glat>38.08469095792561</glat>
      <glon>23.680233657360077</glon>
      <title>sterea</title>
      <temp>45°C</temp>
      <dailyRain>0.0 mm</dailyRain>
      <windSpeed>97  km/hr</windSpeed>
      <fire>3</fire>
      <outsideHumidity>99 %</outsideHumidity>
      <link>
    http://this_is_the_other_link.com
 </link>
  </item>
Community
  • 1
  • 1
marYOs
  • 17
  • 5
  • in your structure there are thee node in link element ( inc 2 text nodes with new lines ). Why you didn't use innerHTML for example in such case ?3 – Vasiliy vvscode Vanchuk Mar 24 '14 at 18:42
  • @Vasil Vanchuk. I just edit the code using innerHTML.Any idea how i'm gonna have a succesfull parse with the element? – marYOs Mar 24 '14 at 19:08

2 Answers2

0

I guess the problem is happening because you're not parsing the XML response correctly...

Try this :

downloadUrl("moredata.xml", function(data) {
  xmlDoc=data.responseXML;
  var items = xmlDoc.documentElement.getElementsByTagName("item");

  for (var i = 0; i < items.length; i++) {
    var link = items[i].getElementsByTagName("link")[0].innerHTML;

  }
})
Héctor William
  • 756
  • 6
  • 24
0

After a lot of searching i find out that in the element<link> the url contains ampersand character --> &.
Ampersand( & ) is a special character for xml. So i replaced ' & ' --> ' &amp; ' .
But i cannot edit the dynamic xml file.
Here is a similar question : how to escape xml entities in javascript?

I'm trying to create a function in my javascript file to replace dynamic , for all url's of the <link> elements the ' & ' to ' &amp; '.
Any ideas , suggestions , examples would be appreciated!

Community
  • 1
  • 1
marYOs
  • 17
  • 5