1

I'm trying to parse a xml from tht thetvdb.com, the problem is that some tv series don't have a name in english so the tags look like this: http://www.thetvdb.com/api/92DBDD8C9816A59C/series/256618/

you only have </SeriesName> and the <SeriesName> is missing.

when I try to print the names like this: console.log("name: " + serieNameT[0].childNodes[0].nodeValue);

my code stops in series like that and the error is 'TypeError'.

how can I pass this elements without problems?

thanks!

1
  • I see <SeriesName/>, not </SeriesName> in the file. Self closing tags are valid in XML. It looks like your problem is that this empty node does not have a childNodes attribute or it is empty and hence you cannot access the first element. It should not be difficult to test whether serieNameT[0].childNodes[0] exists. Commented Mar 1, 2012 at 21:21

1 Answer 1

3

You need to make sure the childNodes exist before referencing them.

if (serieNameT[0].childNodes.length > 0)
    name = serieNameT[0].childNodes[0].nodeValue;
else
    name = "Default series name when empty tag";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.