Utilizing the power of $.ajax() to parse through an XML document

I have a query about working with a large XML file containing 1000 nodes. Here is the code snippet I am using:

$.ajax({
                        type: "GET",
                        cache: false,
                        url: "someFile.xml",
                        dataType: "xml",
                        contentType: "text/xml",
                        success: function(xmlHttpRequest)

Here is an example of the XML structure:

<hospitals>
  <hospital>
    <id>1</id>
    <name>H1</name>
    <city>Riyadh</city>
    <tel>1234567</tel>
    <coordinates>27.034052,49.490662</coordinates>
  </hospital>
</hospitals>

My question is: Is there a way to filter the XML file directly (e.g., by city) without having to read the entire file and then manually do the filtering? I believe there might be a field in the AJAX call that allows for this, but I am struggling to find it.

Answer №1

utilize parse XML in this manner:

let xmlData = "<hospitals>\
  <hospital>\
    <id>1</id>\
    <name>H1</name>\
    <city>Riyadh</city>\
    <tel>1234567</tel>\
    <coordinates>27.034052,49.490662</coordinates>\
  </hospital>\
</hospitals>",
    xmlDoc = $.parseXML(xmlData),
    $xml = $(xmlDoc),
    $city = $xml.find("city").text();

if($city == 'Riyadh')
    alert($xml.find("tel").text());

http://jsfiddle.net/9JUNK/3/

alternatively, you can employ dataFilter to refine the response and then handle it within the success callback

dataFilter(data, type)Function
A function for processing the raw response data from XMLHttpRequest.
This acts as a pre-filtering method to sanitize the response.
You should return the sanitized data. The function takes two arguments:
The original data received from the server and the 'dataType' parameter.

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

Changing the function to operate asynchronously

How can I convert the following code into an asynchronous function? It is currently returning referralUrl as undefined: controller async createReferralUrls() { this.referralUrl = await this.referralService.generateReferralUrl(this.userData.referral ...

Illustrate an element positioned beneath a child element within a different element

I am in the process of designing an overlay on a Google Map that resembles a real map placed on a table, using 2 pixel lines as creases above the map. This design does not significantly impact interactions with the map; only 6 out of 500 vertical lines are ...

Having trouble capturing screenshots with PuppeteerJS?

I've encountered an issue while working with Puppeteer to capture screenshots from a provided URL. The code I have below doesn't seem to be functioning properly. It keeps showing the error message: [0] Error: Protocol error (Emulation.setDeviceM ...

Displaying markers and coordinates in the center circle of a Google Map using Vue.js

Is there a way to display the markers that fall within the specified radius on my map? I need to showcase these locations based on their proximity to a central point, as this will be essential for developing a function that identifies places within a certa ...

Executing an Ajax call to interact with multiple PHP files that are

Currently, I am executing an ajax POST request to a specific file written in php. Let's refer to this file as A.php. This A.php file then proceeds to call another php file named B.php. $.ajax({ type: "POST", url: "../../page_ ...

Unable to access the newly created object's properties following the instantiation of a new resource in AngularJS

Currently, I am in the process of developing a new Resource utilizing AngularJS that falls under the category of Person. After successfully creating this resource, my goal is to retrieve the id associated with the new resource from the server. it('sh ...

Sending Form Data to Zend Framework 2 Controller Using Ajax

UPDATE- The solution has been provided below. I'm struggling to comprehend where and how ZF2 sends form data after clicking a submit button. Whenever I use if ($this->getRequest()->isPost()){ following the ajax call below, it indicates ...

Guide to generating an array entry for every line of a text file in node.js

Struggling with converting each line of a text file into an array entry in node.js The array I am working with is named "temp." The code below successfully prints out each line: var temp = []; const readline = require('readline'); const fs = re ...

The poster image functionality in videos is experiencing issues after updating to version 1.5.0 of the Azure Media Player

I've encountered a strange issue after updating my azure media player to version 1.5.0 - it's not displaying the poster image like it did in version 1.3.0. Below is the code I'm currently using: <div class="marginBlock" id="mediaPlayer" ...

Add opening and closing HTML tags to enclose an already existing HTML structure

Is there a way to dynamically wrap the p tag inside a div with the class .description-wrapper using JavaScript or jQuery? This is the current html structure: <div class="coursePrerequisites"> <p> Lorem ipsum.. </p> </ ...

Issue: $injector:unpr Unrecognized Provider: itemslistProvider <-

I've spent several days debugging the code, but I can't seem to find a solution. I've gone through the AngularJS documentation and numerous Stack Overflow questions related to the error, yet I'm still unable to identify what's caus ...

Contrasting the use of jQuery versus AJAX for updating static page text

While I haven't fully grasped the concept of AJAX yet, my understanding is that it can be beneficial for updating specific parts of a webpage. Does using AJAX only make sense when you require server interaction? I am looking to replace text on a webp ...

Load jQuery core asynchronously with a backup plan

Exploring performance optimization and non-blocking scripts in the header, my focus has been on asynchronously loading jQuery itself. In my search, I came across a jQuery Loader script that can async load jQuery and then handle and queue jQuery document r ...

JQuery isn't functioning properly on dynamically generated divs from JavaScript

I'm currently tackling an assignment as part of my learning journey with The Odin Project. You can check it out here: Despite creating the divs using JavaScript or jQuery as specified in the project requirements, I am unable to get jQuery's .hov ...

Issues with ASP.NET 2.0 callbacks persisting in Firefox browsers

I have developed a .NET Web control that utilizes the callback structure implemented in ASP.Net 2.0. This control, an autodropdown, functions flawlessly in IE 6.0/7.0 and Google Chrome. Below is the pertinent callback function: function ReceiveServerData( ...

Automatically format text fields to display time in hh:mm format from right to left as you type

Is there a way to automatically format hh:mm as the user types in a text field? The default format is 00:00, and I would like it to fill the minutes part when the first two characters are entered, followed by filling the hour part with the third and four ...

An unforeseen vow in lieu of an assortment

I'm currently working on a project involving node and mongo. To achieve parallel requests, I am utilizing a netlify serverless function that will be built using data from mongo records. Here's what I have done so far: paralellNum = 2; const filt ...

When we typically scroll down the page, the next section should automatically bring us back to the top of the page

When we scroll down the page, the next section should automatically bring us back to the top of the page without having to use the mouse wheel. .bg1 { background-color: #C5876F; height: 1000px; } .bg2 { background-color: #7882BB; height: 1000px; } .bg3 ...

Invalid element type detected. A string was expected, but instead found undefined

Following a recent update of next-auth, an unexpected error has surfaced: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your compon ...

The async module has already been invoked with a callback function

Below is an array that I am working with: var files = [ { name: 'myfile.txt' }, { name: 'myfile2.txt' } ]; My goal is to access these objects asynchronously and send them for extraction, as shown below: Extraction function: ...