0

Im parsing an xml file with javascript like this :

x = xmlDoc.getElementsByTagName("Name")[0].childNodes[0];

Everything works as expected, except that some times when i dont have an entry "Name" in the xml it shows this error :

xmlDoc.getElementsByTagName("Name")[0] is undefined

which crash my website. Is there any way to check if xmlDoc.getElementsByTagName("Name")[0] has a value and then continue to parse this value to a variable?

3 Answers 3

1

use

if(xmlDoc.getElementsByTagName("Name") != 'undefined' && xmlDoc.getElementsByTagName("Name")[0] != 'undefined'){
// your code
}
Sign up to request clarification or add additional context in comments.

Comments

1
check this code:-

if (typeof(xmlDoc.getElementsByTagName("Name")[0]) != "undefined" && xmlDoc.getElementsByTagName("Name")[0] != null){

//ur code
}

Comments

0

Use typeof like this:

if (typeof xmlDoc.getElementsByTagName("Name")[0] !== 'undefined') {
  // not undefined
}

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.