Hello Universe
Hello Universe

Reputation: 3302

How to retrieve data from array where inside the array you have xml data?

Imagine I have array as below

myArray holds two values as

0: <maintenanceshortterm type="System.Int32">1111</maintenanceshortterm>
1: <maintenanceshortterm type="System.Int32">1234</maintenanceshortterm>

I want to retrieve the values 1111 and 1234 using jQuery/JS only.

Please please help me how I can retrieve values like that? You can probably guess that that values inside the array are actually coming through xml.

Upvotes: 0

Views: 30

Answers (2)

wade king
wade king

Reputation: 363

also you could make use of the string split mehtod

var arr = new Array('<some xml ...>1111</>', '< some xml...>1234</>');

//split the first string by '>'
var firstItem = arr[0].split(">");
//firstItem now contains "<some xml ...", "1111", ""
//want item at index 1
document.writeln(firstItem[1]);

//likewise for the second value

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370679

Assuming each item is only a maintenanceshortterm and there's no nested structure or anything, a simple regex would do it:

const arr = [
'<maintenanceshortterm type="System.Int32">1111</maintenanceshortterm>',
'<maintenanceshortterm type="System.Int32">1234</maintenanceshortterm>'
];
console.log(
  arr.map((str) => str.match(/>([^<]+)</)[1])
);

Upvotes: 3

Related Questions