I'm attempting to retrieve external XML data and showcase it on my HTML page

My goal is to extract any xml tag from the provided sample. However, I am facing an issue with Internet Explorer where I can only retrieve data from the first record. What kind of javascript code should I implement to resolve this problem?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Grabbing Any XML Tag</title>
<script type='text/javascript' src='http://imaginationeverywhere.info/jslib//dev/jquery-1.5.1.js'>
</script>
<script type="text/ecmascript">
    function getData() {
        var XMLHTTPRequestObject = false;

        if (window.XMLHTTPRequest) {
            XMLHTTPRequestObject = new XMLHttpRequest();

        } else if (window.ActiveXObject) {
            XMLHTTPRequestObject = new ActiveXObject('Microsoft.XMLHTTP');
        }

        if (XMLHTTPRequestObject) {
            XMLHTTPRequestObject.open('GET', 'http://imaginationeverywhere.info/xml/cd_catalog.xml', true);

            XMLHTTPRequestObject.onreadystatechange = function () {
                if (XMLHTTPRequestObject.readyState == 4 && XMLHTTPRequestObject.status == 200) {
                    var xmlDocument = XMLHTTPRequestObject.responseXML;
                    displayArtist(xmlDocument);
                }
            }
            XMLHTTPRequestObject.send(null);
        }
    }

    function displayArtist(xmldoc) {
        titleName = xmldoc.getElementsByTagName('TITLE');
        artistName = xmldoc.getElementsByTagName('ARTIST');
        countryName = xmldoc.getElementsByTagName('COUNTRY');
        companyName = xmldoc.getElementsByTagName('COMPANY');
        priceName = xmldoc.getElementsByTagName('PRICE');
        yearName = xmldoc.getElementsByTagName('YEAR'); 

        displayTitle = "The name of this song title is " + titleName[0].firstChild.nodeValue + ".";      
        displayArtist = "The name of the artist is " + artistName[0].firstChild.nodeValue + "."; 
        displayCountry = "The name of the artist country is " + countryName[0].firstChild.nodeValue + "."; 
        displayCompany = "The name of the artist company is " + companyName[0].firstChild.nodeValue + "."; 
        displayPrice = "This song costs $" + priceName[0].firstChild.nodeValue + "."; 
        displayYear= "The year this song was released " + yearName[0].firstChild.nodeValue + ".";

        var target = document.getElementById('targetDiv');
        target.innerHTML = displayTitle + "<br/>" + displayArtist + "<br/>" + displayCountry + "<br/>" + displayCompany + "<br/>" + displayPrice + "<br/>" + displayYear;
    }
</script>
</head>
<body>
<h3>Get Tag Value</h3>

<form action="#">
    <input type="button" value='Get the name of the artist' onclick="getData()" />
</form>
<div id="targetDiv" style="width:300px; height:20px">

</div>
</body>
</html>

Answer №1

If you're looking for a reliable solution that works across all browsers, I would recommend using jQuery.

Feel free to try out this code snippet:

 function fetchData() {
    $.ajax({
        url: 'ajax/test.html',
        dataType : 'xml',
        success: function(data) {
            handleData(data);
    });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigate back to the previous page upon form submission to a PHP file, with a div dynamically populated with targeted content

I'm facing a logic dilemma: I have a webpage with a div called #content, where I've embedded another page containing a form for submitting data* (there are other pages loaded into #content as well, but they are not active at the same time, one o ...

Create a placeholder for the module function

Update: Providing more specific details. Our team has developed a Github API wrapper extension and we are looking to test different use cases for it. However, we prefer not to use the API wrapper extension directly during testing and instead want to stub ...

Error: Unable to locate Buffer2

I encountered the error Uncaught TypeError: Buffer2 is undefined when trying to import jsonwebtoken into my React project. The errors occur with both import jwt from jsonwebtoken and import {decode} from 'jsonwebtoken'. My versions are jsonwebt ...

"Confusion arises when handling undefined arguments within async.apply in the context of async

While working on my project, I encountered a strange issue with the async library. Some of my arguments end up being "undefined" in my function calls. For example (this is just simplifying my problem): var token; async.series([ function getToken (do ...

Why does the value of my input get erased when I add a new child element?

I seem to be facing an issue when I try to add an element inside a div. All the values from my inputs, including selected options, get cleared. Here's a visual representation of the problem: When I click the "Add Key" button, the text in the textbox ...

Is there a way to retrieve the properties of another function within the same component?

I am trying to place NumberFormat inside OutlinedInput and I also need different properties for the format of NumberFormat. (There will be a select window that defines which format property to use). This is what I have: import OutlinedInput from "@ma ...

Create custom components by wrapping npm components and exporting them as a single custom component

Encountering issues while installing 3rd party components from npm. For instance, a dropdown react module that can be used easily in my own module; however, I find myself needing to declare its style and other dependencies in multiple modules. For example ...

Is it possible to refresh a table that is currently displayed in a model popup?

My code to reload a table or div inside a model popup continuously is not working as expected. Can you help me with this issue? I attempted to utilize ajax for this purpose but unfortunately, the code below is not updating chats (I am working on a chat ta ...

Error encountered while attempting to install material-ui v3.0.3 due to an unexpected termination of the JSON input

I'm currently in the process of installing the most recent stable version of material-ui(v3.03) by running: npm install @material-ui/core. However, I encountered an issue with npm ERR! Unexpected end of JSON input while parsing near '...-/brcast- ...

Changing array indices in JavaScript by splicing elements

I'm experiencing a curious issue that seems to involve overwriting array indices after splicing, or at least that's what I suspect. This problem arises in a small game project built using phaser 2 - essentially, it's a multiplayer jumping ga ...

Utilizing the Facebook Conversion Tracking Pixel within an Ajax-powered Application

Encountering an issue with Facebook tracking. I am trying to trigger the FB tracking pixel when a user signs up for an account. After launching the landing page, only half of the new users are being tracked. Any suggestions on where the problem might be? ...

Timing feature utilized in a Web Application

My latest project involves developing a web-based testing application using CakePHP. Here's how it works: when a user starts a test, the exact start time is stored on the web server. Once the test is completed and the answers are submitted, the serve ...

tips for using Node Mailer to send emails without using SMTP

Currently, I am facing an issue with sending emails through nodemailer. Although I have successfully used my gmail account for this purpose in the past, I now wish to switch to using my business email to communicate with clients on a regular basis. The cu ...

Connecting Ag Grid with modules

Unable to link with modules as it's not a recognized attribute of ag-grid-angular <ag-grid-angular #agGrid style="width: 100%; height: 100%;" id="myGrid" class="ag-theme-balham" [modules]="modules" [columnDefs ...

How can you turn off CSS3 animations for browsers that don't support them

Upon page load, a fade-in animation is applied to the main container. Although it functions properly in most browsers, it seems to have an issue in IE(9). Is there a method to identify if the user's browser does not support CSS3 animations and disabl ...

I am receiving an error stating "Page not found" when making an AJAX request. What could be causing

Whenever I attempt a POST request, I consistently receive the 'Requested page not found. [404]' error message. I am unable to identify the root cause of this issue, so please provide your guidance and advice. I have developed a web API using ASP ...

Prevent the need to go through the keycloak callback process each time the page is

I have integrated keycloak as an identity provider into my React application. I successfully added the keycloak react dependency via npm. Below are the versions of the keycloak react npm modules on which my application depends : "@react-keycloak/web ...

Implementing the adding functionality within an ejs file

I'm currently working on a project that involves fetching data from an API using a simple JavaScript file upon clicking a button. Initially, everything was functioning properly when both the HTML file and JS file were in the same folder, and I could a ...

Incorporating Node.JS variables within an HTML document

After building a simple site using Express, I discovered that Node.js variables can also be used with Jade. exports.index = function(req, res){ res.render('index', { title: 'Express' }); }; This is the code for the index file: ext ...

Cannot locate module: Unable to resolve 'encoding' in '/Users/dev/node_modules/node-fetch/lib'

Currently, I am working with next.js version 13.4.5 and firebase version 10.1.0. Every time I execute npm run dev, a warning is displayed initially. Eventually, an error message pops up in the terminal after the warning persists for some time. I am u ...