Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:58:04+00:00 2026-05-24T03:58:04+00:00

I am learning this stuff so my code might not be pretty… but would

  • 0

I am learning this stuff so my code might not be pretty… but would appreciate some help 🙂

I have not written the following code but got it from somewhere else off the web:

function text_xml()
{
    realXmlUrl="http://jumac.com/del_me_fruits.xml";
    http_request = false;
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType)
    {
        http_request.overrideMimeType('text/xml');
    }

    http_request.onreadystatechange = this.response_xml;
    http_request.open('GET', realXmlUrl, true);
    http_request.send(null);
    xmlDoc = http_request.responseXML;
}

function response_xml()
{
    if (self.http_request.readyState == 4)
    {
        document.getElementById("ex").appendChild(document.createTextNode(" Done!"));
        getFruits(http_request.responseText);
    }
}

function getFruits(xml) {
  var fruits = xml.getElementsByTagName("fruits")[0];
  if (fruits) {
    var fruitsNodes = fruits.childNodes;
    if (fruitsNodes) {
      for (var i = 0; i < fruitsNodes.length; i++) {
        var name = fruitsNodes[i].getAttribute("name");
        var colour = fruitsNodes[i].getAttribute("colour");
        alert("Fruit " + name + " is coloured " + colour);
      }
    }
  }
}

And the error I am getting is:

Error: xml.getElementsByTagName is not a function

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:58:05+00:00Added an answer on May 24, 2026 at 3:58 am

    responseText is a string, not an XML. Are you looking for responseXML?

    Update

    If your script is loaded from a different domain than the XML document you’re loading (http://jumac.com/del_me_fruits.xml), then XMLHttpRequest will act differently depedning on the browser.

    On IE 8, it will pop up a warning window complaining that “The page is accessing information that is not under its control. This poses a security risk. Do you want to continue?” if you click yes, then it will work correctly (i.e., the XML will load and the alerts for the fruits will be displayed).

    On Chrome 12, however, it doesn’t pop anything and it will say that “XMLHttpRequest cannot load http://jumac.com/del_me_fruits.xml. Origin http://localhost:54671 is not allowed by Access-Control-Allow-Origin.” Because of this error, the responseXML property of the request object will be null and you’ll see the error you have.

    There are other questions regarding cross-domain XMLHttpRequest where you may find how to solve your issues, such as Cross-site XMLHttpRequest and http://code.google.com/chrome/extensions/xhr.html.

    <body>
        <script type="text/javascript">
            function text_xml() {
                realXmlUrl = "http://jumac.com/del_me_fruits.xml";
                http_request = false;
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType) {
                    http_request.overrideMimeType('text/xml');
                }
    
                http_request.onreadystatechange = this.response_xml;
                http_request.open('GET', realXmlUrl, true);
                http_request.send(null);
                xmlDoc = http_request.responseXML; // this doesn't have anything
            }
    
            function response_xml() {
                if (self.http_request.readyState == 4) {
                    document.getElementById("ex").appendChild(document.createTextNode(" Done!"));
                    getFruits(http_request.responseXML);
                }
            }
    
            function getFruits(xml) {
                var fruits = xml.getElementsByTagName("fruits")[0];
                if (fruits) {
                    var fruitsNodes = fruits.childNodes;
                    if (fruitsNodes) {
                        for (var i = 0; i < fruitsNodes.length; i++) {
                            var name = fruitsNodes[i].getAttribute("name");
                            var colour = fruitsNodes[i].getAttribute("colour");
                            alert("Fruit " + name + " is coloured " + colour);
                        }
                    }
                }
            }
        </script>
    
        <input type="button" value="Click me" onclick="text_xml();" />
        <p><div id="ex"></div></p>
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning python for some time now. While starting this learning python
I am learning JPA from this tutorial . I have some confusions in understanding
I have written some code that makes use of an open source library to
I am just learning this stuff so please forgive if my code or questions
I'm still learning this stuff, but for the most part, my understanding is that
I have never written a single line of Haskell code (except for random stuff
I'm learning this right now so the questions might be a little juvenile. Here's
I'm learning by reading this tutorial: Link Here's the code: <?php require_once 'Zend/Loader.php'; class
Im learning lisp and im pretty new at this so i was wondering... if
In this book, I am learning how the book writes a stack, but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.