Need help with decoding XML namespaces?

How can I use JavaScript/Ajax to parse values from the following XML snippet?

<yweather:astronomy sunrise="6:34 am"   sunset="8:38 pm"/>

I've been attempting to retrieve the sunrise attribute with no success using this code:

var response = transport.responseXML.getElementsByTagName("channel");
sunrise = response[0].getElementsByTagName("yweather:astronomy").item(0).Attributes["sunrise"].Value;

Unfortunately, my code is not working as expected. Any help would be greatly appreciated! Thanks.

Answer №1

A specialized version of the getElementsByTagName method exists for handling namespaces: getElementsByTagNameNS.

Here is an example:

var result = document.querySelectorAll("response")[0].getElementsByTagName("channel");
var population = response[0].getElementsByTagNameNS("[Namespace URI]", "demographics")[0].getAttribute("population");

In this case, the [Namespace URI] refers to the namespace URI for the citydata namespace.

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

Determine whether a WebElement contains a particular content within the :after pseudo class

After locating my element in Selenium, I've come across an interesting challenge. IWebElement icon = box.FindElement(By.ClassName("box-icon")); Sometimes, this element (icon) has a content set as follows: &:after { content: $icon-specia ...

Experiencing slow loading times with a Next.js app in the development environment

Currently, I am in the process of building a Next.js application and I have noticed that it takes quite a long time to load in the development environment. At the start, the page becomes unresponsive and if I cancel the request, it stops loading altogeth ...

Jquery adds a fun, wobbly effect to animations

I'm experiencing an issue with the animation I've implemented causing some slight shaking and wobbling of the text and certain elements which is affecting the overall look. You can view a live example of this behavior here: This problem specific ...

Modify my JavaScript array by separating each element with a comma

JavaScript or jQuery is what I'm comfortable using Including an additional database table column has resulted in: 122461,4876192 Now there are 2 records So it displays like this: https://i.sstatic.net/7mjFY.png $.each(alldata.Data, function (in ...

Transferring selected radio button values that are dynamically generated by a servlet to a JSP page

I recently incorporated dynamic radio buttons into my JSP page. I now find myself in need of extracting the unique ID of the selected radio button and sending it to another servlet for further processing. How would one go about accomplishing this task? ...

Is there a way to prevent users from right clicking on all links with the same class using js/jquery?

Rails 4 + JS + jquery Is there a way to disable right click on links with the same class in Rails? <% @schedule_hash.values.each do |schedule| %> <%= link_to "Cancellation policy", {:controller => 'web', :action => 'get ...

Creating unique image control buttons for each image within a div using a combination of CSS and JavaScript

How can I create image control buttons, such as close, resize, and rotate, for each individual image when hovering over it? Each image is contained within a draggable div. I want to display an image control button next to each image. This button should on ...

I am encountering an issue where propData in Vue Jest is returning as undefined

I have encountered an issue while passing the propData in my jest test file for a Vue component. It seems that the propData is not being set to the component and instead, I am receiving an error saying "cannot read property of clouds of undefined." Could i ...

Achieving Compatibility Between jQuery 3.6.0 and Node.js v12.16.1

Utilizing an online IDE known as Replit, I am working on node.js projects running on the node version: 12.16.1. However, my current challenge lies in attempting to make jQuery 3.6.0 compatible with this particular node.js version. Despite trying various me ...

"Revamp your site with Vue and API for dynamic background

I'm faced with an issue where I am attempting to modify the background of a joke fetched from an API, but for some reason, the background is not changing and I can't seem to identify why. The main problem lies in my inability to change the proper ...

"Despite the successful execution of the PHP script, the error function in the Ajax POST request

I am working on developing a mobile app using jQuery, jQuery Mobile, and HTML with PhoneGap. I have successfully implemented AJAX to call PHP scripts on the server for tasks like updating, inserting data, and sending emails. However, I consistently encoun ...

Adjust the number of columns based on the minimum screen resolution using columnizer

Currently, I am utilizing the columnizer jQuery plugin to divide my content into columns on a responsive website with a fluid width container. I have implemented different JavaScript functions based on the minimum screen resolutions, similar to CSS media q ...

Dynamic Population of Django Drop Down List using JavaScript

In my Django + Python website, users can request access to a database through a form that provides them with the following options: Environment: A dropdown list with two values - Development and Production. Permission: Another dropdown list with two val ...

Identify when objects in kineticJS are dropped

Key Concept Designing a match-the-following activity where red dots can be dragged to blue dots on the left. The draggable red dots must accurately match with the blue target dots. Objective Assigning values to each dot, and ensuring that once a red do ...

Are you familiar with Vue.JS's unique router options: the 'history' and 'abstract' routers?

Currently, I am developing a VueJS application that involves a 5-step form completion process. The steps are linked to /step-1 through /step-5 in the Vue Router. However, my goal is for the site to return to the main index page (/) upon refresh. One solu ...

Adaptable Semantic UI form design

Welcome, internet friends! If anyone out there has a moment to spare and is familiar with Semantic UI, I could really use some assistance... Currently, I am working on a form that looks great on larger screens like this: https://i.stack.imgur.com/cafc5.j ...

Create a PHP XML parser that will output a message if an attribute is not found within

Imagine having this unique xml structure: <menu_items> <food type="pizza" ingredient="cheese"/> <food type="spaghetti" ingredient="tomatoes"/> <food type="pizza" ingredient="pepperoni"/> <food type="hamburger" ingredient ...

What is the reason behind my POST ajax request being sent as a GET method?

After days of relentless searching, I am finally seeking help for an issue that has been driving me crazy. I attempted to submit a form using an AJAX call with a POST method. However, the $(form).submit() function did not work as expected in my situation. ...

Utilize $validators during blur/focus interactions

In my validation directive, I currently manually set the validation state like this: $element.on('focus', function() { $scope.$apply(function() { ngModelCtrl.$setValidity('length', true); }); }); $element.on('blu ...

Blocking server.js in node.js can be achieved by modifying the code to prevent

Challenge Description In my server.js file, I have included a config file. This is how my server.js file looks: Includes some necessary imports Requires config.js -> makes an API call to get the configuration data using promises Starts the ser ...