Retrieving and showcasing XML data in JavaScript

I have a string that has been parsed from an XML page:

var data = '<Message>Fermata 1431 - Linea 202 -> 08:57 Linea 201 -> 09:02 Linea 256B -> 09:02 Linea 202 -> 09:05 Linea R2 -> 09:06 Linea 201 -> 09:13 Linea 201 -> 09:18</Message>'

I am trying to remove the <Message> tags using pure JavaScript with the following code:

data = data.replace(/<\/?Message>/g, '');

However, I am still seeing the Message tags displayed. It seems like my regular expression is not correct.

Ultimately, I need to display the following information using only pure JavaScript without jQuery:

202 at 08:57
201 at 09:02
256B at 09:02
202 at 09:05
R2 at 09:06
201 at 09:13
201 at 09:18

In PHP, I was able to achieve this with the following code. Now, I need to convert it to JavaScript:

var scraped_data = data.match(/<Message>(.*?)<\/Message>/)[1];
var parts = scraped_data.split('Linea');
    
for (var i = 1; i < parts.length; i++) {
    console.log('<p>Linea ' + parts[i].replace('->', 'at') + '</p>');
}

Answer №1

If you want to give it a shot, consider the steps below:

var data = '<Message>Fermata 1431 -  Linea 202 -> 08:57  Linea 201 -> 09:02  Linea 256B -> 09:02  Linea 202 -> 09:05  Linea R2 -> 09:06  Linea 201 -> 09:13  Linea 201 -> 09:18   </Message>';
var array  = data.match(/[\w\d]+\s*->\s*[\d:]+/g);

for(var j=0; j < array.length; j++) {
  array[j] = array[j].replace('->', 'at');
  console.log('Linea ' + array[j]);
}

Result

Linea 202 at 08:57
Linea 201 at 09:02
Linea 256B at 09:02
Linea 202 at 09:05
Linea R2 at 09:06
Linea 201 at 09:13
Linea 201 at 09:18

Answer №2

Is this what you were looking for?

<script>

var data = '<Message>Stop 1431 - Route 202 -> 08:57 Route 201 -> 09:02 Route 256B -> 09:02 Route 202 -> 09:05 Route R2 -> 09:06 Route 201 -> 09:13 Route 201 -> 09:18 </Message>';

var reg = /([\w\d]+)\s->\s(\d+:\d+)/g;
var matching;
var result = [];
while (matching = reg.exec(data)){
    result.push(matching[1] + " at " + matching[2]);
}
console.log(result);

</script>

Answer №3

To achieve this, simply follow these steps:

<script>

let message = '<Message>Fermata 1431 - Linea 202 -> 08:57 Linea 201 -> 09:02 Linea 256B -> 09:02 Linea 202 -> 09:05 Linea R2 -> 09:06 Linea 201 -> 09:13 Linea 201 -> 09:18 </Message>';
info = message.match(/([\w\d]+)\s->\s(\d+:\d+)/g);
</script>

Upon executing this code snippet, you will have the necessary values stored in the info array.

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

Adding an image within the body of text in a Django model, where both the text and image coexist

I am currently seeking a method to seamlessly insert an image within the text of my Django-powered blog. My goal is to achieve a layout similar to the one showcased in this example: https://i.stack.imgur.com/cFKgG.png The desired layout consists of two c ...

Utilize the MaterialUI DataGrid to showcase nested object values in a visually appealing

Hey there, fellow coders! I'm currently working on fetching data from an API and displaying it in a data grid using React.js. Here's the format of the data I'm receiving from the API: {data: Array(200)} data : (200) [{…}, {…}, {…}, { ...

When enclosing my Javascript code in CDATA tags within my SVG files, it does not execute

Having an SVG file in this format: <svg id="test" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" viewBox="0 0 1435 1084"> &l ...

Difficulties arise when trying to identify the presence of every individual character within a given string

I'm currently working on a software that can count the occurrence of every character in a given string. Here's an example: Enter string: hello world The expected output should look like this: h: 1 e: 1 l: 3 o: 2 ...and so forth However, my c ...

What are the steps to successfully install OpenCV (javascript edition) on Internet Explorer 11?

I'm currently experiencing issues with getting the OpenCV javascript version to function properly on IE11 for contour detection. While my code runs smoothly on all other up-to-date browsers, I am encountering errors such as: TypeError: Object doesn&a ...

Steps to open and configure a Mobile-Angular modal from another controller

Is it possible to use a modal in Angular JS for mobile devices without compromising the layout? I'm having trouble setting up the controller for my modal inside my main controller and passing parameters. Should I consider using ui-bootstrap for this p ...

What could be causing the issue of Vuejs 3.3 defineModel consistently returning undefined?

I am currently working with Nuxt version 3.5.1 and Vuejs version 3.3, however, I am encountering an issue where the defineModel macro always returns undefined. I am unsure why this is happening? <template> <input v-model="count"& ...

Exporting data from a table with an identifier

Is there a way to export the HTML table data so that it retains its original format? The data[] array serves as the source for the table displayed below. By clicking on the dataToArray button, you can export this HTML table data back into an array. Howeve ...

ReactJS sidebar navigation functionality

I have a fixed sidebar with icons for navigating to different pages. When an icon is clicked, a secondary menu slides out. However, the issue is that when another icon is selected, the menu slides back in instead of switching or staying out. Additionally, ...

Ways to access reference data within the parent data map in Firebase

I'm having trouble making this code work properly. The issue I'm encountering is that the res.data() does not appear in the docs object. getProjects = async () => { const colRef = db.collection('parentCollection').orderBy('c ...

What steps can be taken to adjust a module required in Node.js prior to initiating the build process?

Module A.js (standard NPM package): module.exports = { ... const wsUrl = `ws://${customHostname}:${customPort}/sockjs-node/websocket`; ... }; Module B.js: const a = require('A'); Is there a way to substitute ${customHostname} & ${cu ...

How come the transition does not take effect when removing and adding the class to the same element with the removeClass() and addClass() methods?

Two images are present, with the first one having the class "opacityOne". When a button is clicked, based on the variable index, I want the current image to fade in while the other fades out. It works well when I remove the "opacityOne" class from one ima ...

Update the image links to automatically refresh every half a second based on the values inputted in the text bars

Is there a better and simpler way to define AAAAAAAAAA, BBBBBBBBBB, and CCCCCCCCCC using the links provided in the text bars named chart1, chart2, and chart3? This learning project is being done through Notepad++ for personal use only, with no need to sa ...

Dynamically allocate a controller to an element following the Bootstrap process

I have a unique AngularJS application that is initialized manually at a specific time. Later on, I need to display an HTML element without any ng-controller assigned to it. Is there a way to dynamically add an ng-controller to this element in order for it ...

I am puzzled by the behavior of the $.getJSON request. I am uncertain about how to properly format the request with the callback=? parameter

Out of the three jQuery JSON requests, one is encountering cross-domain errors due to my lack of understanding how to include the callback=? parameter (or the reason why it signifies JSON vs JSONP). I am working on two requests to the same API; however, o ...

Employing the 'this' keyword for iterating over a jQuery selector

Having an issue with a jQuery code that logs the values of input fields on button click: Tried using forEach method on inputs but getting an error saying it's not a function. Why is this happening? $("#submit").click(e => { e.preventDefault(); ...

An easy way to enable mobility for BootstrapDialog on mobile devices

Currently, I am utilizing the library available at https://github.com/nakupanda/bootstrap3-dialog in order to create a dialog box. However, my issue arises when viewing the dialog on mobile devices where it exceeds the screen size. On desktops, adjusting t ...

Alternative solution to fix navigation issue in Flex 4.5 when navigatetoURL is not functioning as intended

Perhaps you are aware of the compatibility issues that Google Chrome and Safari have when using navigatetoURL, as it only works in Internet Explorer. To address this problem, I found a code snippet on a forum which consists of a JavaScript function embedde ...

Using the event object with fullCalendar.formatDate: A step-by-step guide

I am struggling to implement the formatDate function with the event object retrieved from the eventMouseOver method and it doesn't seem to be working as expected. Do you have any recommendations? Below is the snippet of my code: eventMouseover: func ...

Is it possible for a JavaScript .execute function to be executed synchronously, or can it be transformed into an Ajax

Currently, I am utilizing the ArcGIS API for Javascript which can be found at this URL: The JavaScript code snippet I'm working with appears to be running asynchronously. Is there a way to convert it to run synchronously or potentially transform it i ...