Converting XML file data into an array using AngularJS 4

How can I parse XML data into an array in AngularJS 4?

Here is an example of the XML data:

<data>
<element>ESA</element>
<element>XCAVS</element>
<element>TQSZ</element>
<element>HGDQZ</element>
<element>OMGDS</element>
</data>

I would like to extract the data from the element tags (such as ESA, XCAVS, TQSZ) and convert it into an array using AngularJS 4.

Can someone guide me on how to achieve this?

Answer №1

If you need to parse the XML mentioned above, you can achieve this using standard JavaScript by following the code snippet provided below:

var parser, xmlDoc;
var text = "<data>"
+ "<element>ESA</element>" +
  "<element>XCAVS</element>" +
  "<element>TQSZ</element>" +
  "<element>HGDQZ</element>" +
  "<element>OMGDS</element>" +
  "</data>";
  parser = new DOMParser();
  xmlDoc = parser.parseFromString(text,"text/xml");

var elements = xmlDoc.getElementsByTagName("element");

var arr = [];
for (i = 0 ; i < elements.length ; i++) {
alert(xmlDoc.getElementsByTagName("element")[i].childNodes[i].nodeValue);
arr.push(xmlDoc.getElementsByTagName("element")[i].childNodes[i].nodeValue);
}

Answer №2

Although this isn't a concrete code solution, it might be beneficial: https://github.com/abdmob/x2js

This incredible library can convert your XML to JSON effortlessly. This makes manipulating your data much simpler and more manageable.

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

What is the best method to eliminate a "0" entry from a javascript event array?

Hello, I've got an array structured as follows: let test = ["testOne:,O,U,0","testTwo:R,C,0","testTree:1.334","testFour:r,z"]; I'm looking to iterate through the array and remove any occurrences of the cha ...

Utilize the power of generics with Angular's service providers

Is it possible to make the membervar of class Parent generic instead of type any, while still retaining the ability to switch provider classes without having to update all components that rely on class Parent? For example, if class ChildB implements a diff ...

Exploring the process of transforming an integer from a text input into an array in Swift 4

I've been working on an iOS app where I'm trying to input an integer into an array using a textfield and a button, in order to store it using 'userdefaults'. Currently, I have a textfield that adds the entered String into the array and ...

Tips for positioning the calendar icon inside the input field using Angular Material

Just beginning my journey with Angular Material and currently focusing on building a datepicker. I am looking to reposition the icon from outside of the input field to inside. After trying out various Material themes, none seem to have this specific style. ...

When working with the "agora-rtc-sdk-ng" package, an error may be thrown by Next.js stating that "window is not defined"

Currently, I am in the process of incorporating the "agora-rtc-sdk-ng" package for live streaming with next.js and typescript. import AgoraRTC from 'agora-rtc-sdk-ng'; However, when I try to import it, an error is thrown as shown below: https:/ ...

Gulp is failing to create a JavaScript file from TypeScript

Below is the Gulp task I am using: var gulp = require('gulp'); var typescript = require('gulp-typescript'); var sourcemaps = require('gulp-sourcemaps'); var tsProject = typescript.createProject('tsconfig.json'); var ...

Angular2 - Integration and utilization of the datepicker component

Is there a way to retrieve the selected date from an input and wrap it in a <p> tag? I've been attempting to use the jQueryUI datepicker and have tried binding events like change and click, but haven't had any success. Can anyone offer som ...

I encountered an issue trying to run my Angular project on multiple computers

We are currently transitioning to a team-based approach for our Angular project, as a new developer has recently joined. However, we are facing some challenges in getting the project up and running on the new computer. We need assistance troubleshooting th ...

"Disabling border-radius for Bootstrap input fields in Angular: A step-by-step guide

I've been using Bootstrap input controls and attempting to remove the border by setting it to 0px, but unfortunately it's not working as expected. Despite looking at numerous posts on this topic, none of the solutions seem to be effective for me ...

Performing two rounds of filtering on a NumPy array and then

Is there a more efficient way to execute two filtering steps and one replacing step on a large array without the need for making copies or using loops? For example, consider two numpy arrays of different sizes. I want to apply two filters. The first filte ...

Passing a dynamic JSON array to a Highcharts Pie Chart can be achieved by incorporating

I have successfully passed a json encoded string (e.g. $TEXT2 consisting of ["chrome","15","firefox","20"]) from Xcode to a JavaScript array (e.g. arr). Now, my goal is to dynamically pass this array containing a json string to a Highcharts Pie. The corres ...

Error: Attempting to access the value property of a null object within a React Form is not possible

I am currently developing a form that includes an HTML input field allowing only numbers or letters to be entered. The abbreviated version of my state interface is outlined below: interface State { location: string; startDate: Date; } To initiali ...

Is the "ngIf" directive not functioning properly within a "ngFor" loop in Angular?

I am in the process of developing an e-commerce website that focuses on selling a variety of shirts. Currently, I have set up a system where all the available shirts are displayed using an *ngFor loop. My goal is to create a feature that allows users to cl ...

I encountered an error with the TS1003 code in Angular because an identifier was expected on the throw import statement. Upon further investigation, I realized that the issue originated from

I came across this piece of code: import { Observable, throw} from 'rxjs'; An error message popped up saying identifier expected: ERROR in config/config.service.ts(3,22): error TS1003: Identifier expected. The error appears to be related to ...

A TypeScript class designed to serve as a function type as well

When trying to utilize angular's IHttpService, I am unsure of how to manage the following function. interface IHttpService { <T>(config: IRequestConfig): IHttpPromise<T>; } class MyHttpService implements IHttpService { // The cod ...

I am looking to append a new value to an array within the state in React

development environment ・ react ・ typescript In this setup, state groups are stored as arrays. If you want to add a group to the array of groups when the onClickGroups function is called, how should you go about implementing it? interface ISearc ...

Tips for soothing the TypeScript compiler

It seems like TypeScript is heavily influenced by Microsoft's interpretation of the DOM and JavaScript. But what if I'm not concerned with Internet Explorer and Edge? Unfortunately, I'm encountering issues with TypeScript... For instance, w ...

What is the best way to create an interactive experience with MapLibre GL in Full Screen mode?

Hello, I am seeking a solution to create a map with the interactive option set to false, but once in full screen mode, I need this parameter to be true. Do you have any suggestions or ideas on how to achieve this? const _map = new MapGL({ contai ...

Retrieve numerical values solely from the text containing a specific keyword

I currently have an array with 4 elements, and I am looking to extract integers only from the strings that contain the keyword Approved. my_array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 25000 Regula ...