Why can't xml nodeValue get in javascript?

if (window.DOMParser) {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(txt,"text/xml");
}
else { // Internet Explorer 
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(txt); 
}
                /*copy ends*/
temp = xmlDoc.getElementsByTagName('COMMENT');
s0 = xmlDoc.getElementsByTagName('TITLE')[i].nodeValue;
s1 = xmlDoc.getElementsByTagName('CMT')[i].nodeValue;

s0 and s1 returned null, and I don't understand why?

+3
source share
1 answer

The property nodeValueof XML elements is always nullbecause the contents of an element are actually stored in text nodes inside the element. If the content is simple enough, you can do something like:

s0 = xmlDoc.getElementsByTagName("TITLE")[i].firstChild.nodeValue;
s1 = xmlDoc.getElementsByTagName("CMT")[i].firstChild.nodeValue;
+12
source

Source: https://habr.com/ru/post/1793362/


All Articles