0

I have the following xml structure:

 //xml
<ArrayofTech>
 <TechJobs>a</TechJobs>
 <TechJobs>b</TechJobs>
</ArrayofTech>

I would like to store in array the above TechJobs nodes xml like: Array.push(Techjobs). I have to store each TechJobs node into an array. How to parse this xml .The Code which i have tried is shown below:

$(xml).find("TechJobs").each(function () {
array.push($(this));
}

How to fix this?

1
  • Show us your full code, or make an online demo like on jsfiddle. Commented Apr 17, 2014 at 12:35

3 Answers 3

1

Try to use .outerHTML like,

var xml = "<ArrayofTech> <TechJobs>a</TechJobs> <TechJobs>b</TechJobs></ArrayofTech>";
var array = $(xml).find("TechJobs").map(function() {
    return this.outerHTML; // to get the techjob
}).get();
console.log(array);

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

Using .get() alongside .map to transform the data, should return you the array:

var array = $(xml).find("TechJobs").map(function() {
    return $(this).text();
}).get();

If you want the XML, just use .get and leave out the mapping:

var array = $(xml).find("TechJobs").get();

jsFiddle

2 Comments

@RGraham I want the complete xml node ie TechJobs xml as a string not just the values.
@user3393032 Even easier, just remove the .map function
0

You can use .map() here:

var arr = $(xml).find("TechJobs").map(function() {
    return $(this).html();
}).get();

Fiddle Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.