1

I can't seem to find any solutions in JavaScript as I don't want to work with jQuery. I am trying to get the date text from an xml document. I have the following code, but I get the error that length cannot be read from undefined. What am I doing wrong?

var request, xmldocument, date, xmlDate;
request = new XMLHttpRequest();
request.open("GET", "http://XXXXX/rss.php", true);
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        // xml document
        xmldocument = request.responseXML;
        // dates from xml document
        date = xmldocument.getElementsByTagName("pubDate");
        // loop over dates
        for (count = 0; count <= date.childNodes.length; count++) {
            // text of date node
            xmlDate = date.childNodes[i].nodeValue;
            // read out text
            console.log(xmlDate);
        }
    }
}
request.send();

1 Answer 1

1

getElementsByTagName gives you a nodelist not one element with the results as child nodes. Try

    for (count = 0; count < date.length; count++) {
        // text of date node
        xmlDate = date[count].childNodes[0].nodeValue;
        // read out text
        console.log(xmlDate);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

great it worked and displays dates but I also get the error "Uncaught TypeError: Cannot read property 'childNodes' of undefined" for the line xmlDate = date[count].childNodes[0].nodeValue;

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.