Using SimplyJS and XML: extracting the value of a specific key

Hey there,
I wanted to update you on my progress with the Pebble Watch project. I've switched over to using an official external API to make HTTP requests for values, and now I'm receiving results in XML format instead of JSON.
Here's a snippet of the XML response that I get from calling the API:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <items>
      <item id="1234567">
         <author gender="man" country="France" region="Ile-de-France">myuser</author>
         <category>mycategory</category>
         <date>2010-08-23T05:48:51+02:00</date>
         <from />
         <text>THIS IS THE CONTENT</text>
         <comments_flag>1</comments_flag>
         <short_url>http://myshort.url</short_url>
      </item>
   </items>
   <comments />
   <active_key>1234567890</active_key>
   <code>1</code>
   <pubdate>2014-07-12T18:51:49+02:00</pubdate>
   <language>fr</language>
   <errors />
</root>

Now, here's what my SimplyJS script looks like:

simply.on('accelTap' || 'singleClick', function(e) {
      simply.body("Loading...");
      ajax({ url: 'http://myAPIurl', type:'xml' }, function(data){
      simply.scrollable(true);
      simply.body(data);
    });
  //}
});


Currently, the entire XML is being displayed on my Pebble. However, I'm only interested in extracting the content under the < text > section.
I've attempted to use some selectors like "data.text," similar to how it works with JSON, but I keep encountering object errors.

Thanks so much for any help!

Answer №1

Thanks to Pedrolane, a helpful member of the Pebble forums, I was able to find the solution.
This one-liner did the trick:

var mytext = data.match(/<text>(.*?)<\/text>/)[1];

To make the text more readable, simply replace any occurrences of '& quot;' with '"' :

mytext = mytext.replace(/&quot;/g, "\"");

For more information, consult the Simply.JS Documentation

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

Encountering the "Invalid Element Type" error in a Vue Native project right after setting it up

After creating a vue-native project with the command vue-native init henry-pager, I navigated to the directory and initiated the online builder by running expo start. However, when attempting to run it on the web platform, an error message appeared: Error: ...

Executing php class method through ajax with jQuery without requiring a php handler file

How can I trigger a PHP class method using AJAX with jQuery without the need for a separate PHP handler file? Here is my PHP Class animal.php:- <?php class animal { function getName() { return "lion"; } } ?> jQuery code snippet:- ...

What is the procedure for selecting an element based on its child containing specifically filtered text?

Imagine a webpage with the following elements: <div data-search="type1"> any HTML <!-- .click is a child of any level --> <span class="click" data-source="page1.html">blue</span> <!-- let's call it "click1 ...

Generate - Create 2 different versions of the web application

Currently, I am in the process of learning Grunt and exploring ways to generate 2 variations of an application with different configuration settings. My goal is to have one version where a boolean in a specific .js file is set to false, and another versio ...

The error message "Required parameter not provided" appeared when trying to utilize a nested dynamic route in Next.js

Issue: The error message indicates that the required parameter (plantName) was not provided as a string in getStaticPaths for /plants/[plantName]/streaming-data/[panel] The error above is being displayed. My folder structure follows this pattern: plants > ...

In search of javascript implementations of the pubhubsubbub protocol that are open source

Can you list out some of the open-source Javascript implementations for the PubSubHubbub protocol, starting with the publishing side? ...

Response from PHP XML converter

Having recently delved into the world of XML, I find myself working in PHP and making requests to a server that consistently returns responses in XML. Instead of focusing on how I receive the response, I want to discuss how to effectively convert the XML ...

Is there a way to refresh the index data with ajax jQuery?

I'm working with an array of data retrieved via the Ajax GET method. Currently, I am displaying this data using its index number such as data[0]. To access the next set of data, I click on a button that should update the index from "here 0" to fetch d ...

I'm having trouble sending a string to a WCF service using jQuery AJAX. What's preventing me from sending strings of numbers?

Something strange is happening with my web service when using jquery ajax - I can only pass strings containing numbers. This was never an issue before as I would always just pass IDs to my wcf service. But now that I'm trying something more complex, I ...

Exploring the power of AngularJS and Jasmine: testing multiple instances of a service in action

Currently, I am immersing myself in the book "Mastering Web Application Development with AngularJS" and came across an example test named 'Aggregating callbacks.' The specific example causing me troubles involves the Person object: var Person = ...

Python is experiencing difficulties with copying xpath elements

I attempted to utilize some Python code to access Twitter and retrieve the "Happening now" text. Unfortunately, it was unsuccessful. import webbrowser print("Visiting Twitter.com...") webbrowser.get('C:/Program Files (x86)/Google/Chrome/Application/c ...

Data vanishing in upcoming authentication session in test environment

I have encountered an issue with next auth in my next.js project. During development, the session data is lost if the server refreshes or if I switch to another tab and return to it. This forces me to sign out and then sign back in to restore the session d ...

Problem with the WP Rocket helper plugin that excludes JS scripts from Delay JS only at specific URLs

Looking for assistance with a helper plugin that excludes scripts from "Delay Javascript Execution"? You can find more information about this plugin here. The specific pages where I want to exclude slick.min.js and jquery.min.js are the home page and tabl ...

Upon successful registration, users will be automatically redirected to their profile page

Having trouble with the redirection to the login page from my profile page, which is an HTML file and the main page is the login page. I've tried redirecting to both pages, but it always lands in the catch block whenever a redirect is attempted. angu ...

Using JSON to insert an array into an object with identical key name

var arr = ['1', '2', '3'] var part = {} var partContents = [] arr.map(function(i){ partContents.push({ obj: i }) part['text'] = partContents }) console.log(part); Is there a way to create separate arrays with ...

Arrange a JSON array by searching texts initially, followed by sorting the remaining results in alphabetical order

I am looking to sort a JSON array in JavaScript based on a search text. The sorting should prioritize items that match the search text at the beginning of their value, followed by alphabetical sorting for the remaining results. Even if the search text is f ...

Is there a way to associate a click event with an angular2 template interpolation?

Can a click event be bound to an interpolation? Whenever I try to run the code below, I encounter the following ERROR Error: Uncaught (in promise): Error: Template parse errors: Parser Error: Got interpolation ({{}}) where expression was expected at col ...

Form for sending mail using custom AJAX capabilities

Here is the HTML form code I have: <form id="main-contact-form" name="contact-form"> <div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms"> <div class="col-sm-6"> ...

retrieving the value of a field within an array

Here is my code snippet: <div class="label">{{ item.data[0] }}</div> and in the view, this is what I have: { "id": 6, "firtname": "JHON ", "lastname": "SCALA", "fullname& ...

Managing traffic in Google Kubernetes Engine (GKE)

I am encountering an issue with our website deployment on GKE, which consists of 10 pods. When deploying a new version, we use MAXsurge=1 and MAXunavailable=0. Upon trying to access the website during a new deployment, I sometimes only see the header in t ...