1

I have a part of an XML file that looks like this:

<paymentTypes>
<paymentType>type1</paymentType>
<paymentType>type2</paymentType>
<paymentType>type3</paymentType>
<paymentType>type4</paymentType>
</paymentTypes>

The thing is that I want to get it via ajax jQuery, but if I try this:

var paymentTypes = $(xml).find("paymentTypes").text() 

the output will be type1type2type3type4.

What function should I use(built-in or should I write it) in order to get the results on separate lines?

Thanks!

UPDATE: In my XML file there are multiple items, each of them with their own entry.

7
  • what is the desired output? do you need an array of the text values Commented Sep 30, 2014 at 10:44
  • if so var paymentTypes = $(xml).find("paymentType").map(function(){return $(this).text()}).get() Commented Sep 30, 2014 at 10:44
  • @ArunPJohny it should be paymentType, not paymentTypes. Commented Sep 30, 2014 at 10:46
  • The desired output is type1\n type2\n type3\n type4 Commented Sep 30, 2014 at 10:50
  • @Matei_Radu should this output be in console or in page (with <br/> instead of \n)? Commented Sep 30, 2014 at 10:51

1 Answer 1

1

u can iterate over the result append to new string

var xml ='<paymentTypes><paymentType>type1</paymentType><paymentType>type2</paymentType><paymentType>type3</paymentType><paymentType>type4</paymentType></paymentTypes>';

var result = "";
$(xml).find('paymentType').each(function(i,v){
    result += $(v).text()+'\n';
});
console.log(result);
Sign up to request clarification or add additional context in comments.

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.