简体   繁体   中英

XML jQuery get multiple nodes with same name

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM