T
T
TempUserMain2014-05-29 11:34:11
JavaScript
TempUserMain, 2014-05-29 11:34:11

Handling non-existent XML tag when parsing?

The following module is used for parsing: XMLDom
There is an xml-file that can contain the "SomeTag" tag.
It is necessary to check if there is a "SomeTag" tag in the code.
When using the code (there is no "SomeTag" tag in the xml file):

if(doc.documentElement.getElementsByTagName("SomeTag") == null) {
        continue;
}

the error is displayed:
"TypeError: Cannot call method 'getElementsByTagName' of null" Can
you please tell me how to check the presence of a tag in an xml file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TempUserMain, 2014-05-29
@TempUserMain

Gorily
Thank you for your reply.
Issue resolved.
To check if a tag exists, you can use:

if (doc.getElementsByTagName("SomeTag").length === 0) {
        continue;
}

I
Ilya, 2014-05-29
@Gorily

JS is not my profile, but judging by the text of the error, your doc or documentElement is null and you are trying to call a method on it (on null) that does not exist.
Thus, the question is not that getElementsByTagName is not looking for an element, but that you have not initiated something before, a simple query on the error text in Google also indicates this, here are similar problems with solutions:
stackoverflow.com/ questions/20641420/cannot-call-m...
https://developer.appcelerator.com/question/155648...
If you can't figure it out, show a larger piece of code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question