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

Converting JSON to Array: A Step-by-Step Guide

Greetings, StackOverflow community! This is my inaugural question here. I believe the answer is not overly complex, but my knowledge of Javascript is quite rudimentary. Currently, I have a JQuery AJAX function that processes this JSON object: { "Users" ...

Insert metadata tag into the head element of the parent iframe

I am looking to insert a meta tag element into the head element of an Iframe parent. I attempted window.parent.$('head').append('sometext'); This method worked when the source file and iframe file were in the same folder, however it d ...

hyperlink to choose a specific option from a drop-down menu

The challenge I am currently working on involves page1.html <a href="/foo"></a> foo.html <select ng-model="ctrl.listvalues"> <option id="{{item.value}}" ng-repeat="item" in ctrl.availableValues" value="{{item.value}}">item.di ...

Facing an issue where the CSS class name is not displaying correctly when utilizing CSS Modules with my React

This webpack.config.js file is dedicated to the module section, where loaders are defined for JSX files and CSS. module: { loaders: [ { test: /\.jsx?$/, exclude: /node_modules/, loader: 'bab ...

Customize the appearance of your apps script using conditional formatting to highlight values that are

https://i.stack.imgur.com/L1KFZ.png I need to create an array of all 50 US states based on the abbreviations in a column. The goal is to compare each cell against the array and if it doesn't match, format it red. After researching conditional format ...

The personalized directive does not respond to modifications in attributes

I am in the process of creating a modal directive that will be used to display data tables. The directive has an attribute called modal-visible, which is initially set to false by default. If this value is true, the modal will be visible when loaded. Howev ...

Attempting to utilize JavaScript in order to reset a text input field

I'm having trouble clearing a text field using this function. The alert is working but the field remains unchanged. What could be the issue? This function is designed to work on any text field. <!DOCTYPE html> <html> <head> ...

Utilizing jQuery for cloning elements

I need to add functionality for adding and deleting rows in multiple tables on my website. Currently, I am doing this by directly inserting HTML code using jQuery, but it has become inefficient due to the number of tables I have. I am considering creating ...

Transmit information using JSON format in Angular 8 using FormData

i am struggling with sending data to the server in a specific format: { "name":"kianoush", "userName":"kia9372", "email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd7d5ddd8ce85...@example.com</a>" } H ...

Prevent repetitive content on your Node.js server

After realizing my small image hosting has many duplicate content, I am looking for a solution to prevent this issue in the future. My idea is to use either checksum or hash code so that whenever a new file is uploaded, it will be hashed and compared with ...

Error: Unable to access 'push' property of null object in Next.js Link

Utilizing Vite to develop a reusable component has led to an error upon publishing and reusing it: TypeError: Cannot read properties of null (reading 'push') The code for the component is as follows: import React from "react"; import ...

Discover the secrets to easily finding specific items within a table by harnessing the power of the table sorter plugin with

Currently, I am utilizing the tablesorter plugin and its corresponding tablesorter widgets to facilitate searching within a table. However, I encountered an issue with the tablesorterWidgets.js file when attempting to configure the number of rows displayed ...

Tips for tailoring content based on screen size

I am looking for a way to display different content depending on whether the user is viewing my website on a large screen (PC/tablet) or a small screen (mobile). While my site is responsive and uses bootstrap, I have a lot of content that is only suitable ...

"Integrating an AJAX-powered MySQLi update query in a custom WordPress

Currently, I am working on an application based on Wordpress where I am trying to update the status of listed orders using Ajax. However, when I implement the code below, I encounter the following error message: Failed to load resource: the server responde ...

Tips for creating multiple files using nodejs and express

I am currently working on developing a personalized code editor that consists of 3 textareas: html, css, and javascript. The objective is to save the data from each textarea into individual files. With the help of express and nodejs, I have successfully m ...

What is the best way to align an avatar within a cardHeader component in the center?

Recently, I've been working on a frontend project using Material UI. In my design, I have cards that display data, and I wanted to include an avatar in the center of each card. Despite using a card header to insert the avatar, I struggled to align it ...

Dimensions of Bootstrap carousel

I am attempting to create a Bootstrap carousel with full-width images (width: 100%) and a fixed height. However, when I set the width to 100%, the height automatically takes on the same value. I am unsure if the issue lies within my files. <div id="m ...

What are the steps to modify the placeholder of the formik <Field/> component?

Struggling to figure out how to make the date input empty with Formik. I just want it to display nothing initially, but have been unable to do so after hours of trying different methods. <Field name="date" type="date" className ...

Utilizing ASP.NET AJAX: Enhancing User Experience with ScriptManagerProxy for HTML Button Click Events

I am currently working on a solution for the following issue: Managing the client-side click event of a button - preventing postback if the JavaScript call returns false (e.g. onclick="js:return IsThisAllowed()") The JavaScript method will either return ...

Encountered a Socket.io issue: CONNECTION_TIMED_OUT_ERROR

I'm in the process of developing a simple HTML chat program. I've set up a node server for testing, but encountering a socket error when trying to access the HTML page. This is my first experience with Node.js and setting up a server, so it' ...