简体   繁体   中英

Cannot parse <link> element from xml with dom

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>

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;

  }
})

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM