简体   繁体   中英

Parsing XML-files using Javascript in Safari

I want to parse some data from a XML-file using Javascript. It worked fine with Google Chrome, but now I found a problem using Safari. I get an error because of the call xml_genre.children: this element does not contain the information children in Safari, but I can run this in Chrome without problems.

The only solution I found is to use the childnodes, but there I also get some unwanted data, like multiple spaces.

Is there a better solution to iterate through a XML like this using Javascript?

Extract from the XML-file:

<genres>
    <genre>Thriller</genre>
    <genre>Comedy</genre>
</genres>

Extract from the Javascript-code:

var xml_genre = movie_file.getElementsByTagName("genres")[0];
if (xml_genre != null) {
  var genres = new Array();
  for(var i = 0; i < xml_genre.children.length; i++) {
    genres.push(xml_genre.children[i].textContent);
  }
  movie.setGenre(genres);
}

Now I took a different approach. This works in Chrome and Safari as I need it:

var xml_genres = movie_file.getElementsByTagName("genres")[0];
if (xml_genres != null) {
  var xml_genre = xml_genres.getElementsByTagName("genre");
  var genres = new Array();
  for(var i = 0; i < xml_genre.length; i++) {
    genres.push(xml_genre[i].textContent);
  }
  movie.setGenre(genres);
}

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