简体   繁体   English

从XML获取节点值并将其添加到数组?

[英]Getting node values from XML and adding them to array?

My XML looks like this: 我的XML如下所示:

<photo>photo1.jpg</photo>
<photo>photo2.jpg</photo>

I can get the value of each photo node by doing something like: 我可以通过执行以下操作来获取每个照片节点的值:

var myPhoto = $(this).find('photo').text();

I'm trying to get an array of nodes like so: 我正在尝试获得像这样的节点数组:

myPhotoArray.push(myPhoto); 

When I push this to an array how do I add a comma separator for each node? 当我将其推送到数组时,如何为每个节点添加逗号分隔符? It prints all the node values out on one line (photo1.jpgphoto2.jpg) Thank you! 它将所有节点值打印在一行上(photo1.jpgphoto2.jpg)谢谢!

Use join when you output: 输出时使用join

myPhotoArray.join(",");

Actually, the comma is the default separator, so you could leave it out: 实际上,逗号是默认的分隔符,因此您可以省略它:

myPhotoArray.join();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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