使用 Javascript 或 PHP 创建 XML 文件
我有一个 XM
L 文件,如下所示:
<bracelets>
<photo filename="b1.jpg" thumbnail="a1.jpg" description="aa" />
<photo filename="b2.jpg" thumbnail="a2.jpg" description="aa" />
<photo filename="b3.jpg" thumbnail="a3.jpg" description="aa" />
<photo filename="b4.jpg" thumbnail="a4.jpg" description="aa" />
</bracelets>
我想将所有图像名称提取到 .php
页面中。
我现在正在用这个。
$bracelets = simplexml_load_file($bracelets_xml);
x = xmlDoc.getElementsByTagName('photo')[0].attributes;
for (i = 0; i < x.length; i++) {
document.write(x[i].childNodes[0].nodeValue);
}
但目前我只能获取一张图像。
我怎样才能获取所有图像?
I have a XM
L file as follows:
<bracelets>
<photo filename="b1.jpg" thumbnail="a1.jpg" description="aa" />
<photo filename="b2.jpg" thumbnail="a2.jpg" description="aa" />
<photo filename="b3.jpg" thumbnail="a3.jpg" description="aa" />
<photo filename="b4.jpg" thumbnail="a4.jpg" description="aa" />
</bracelets>
I want to fetch all image names into a .php
page.
I am using this now.
$bracelets = simplexml_load_file($bracelets_xml);
x = xmlDoc.getElementsByTagName('photo')[0].attributes;
for (i = 0; i < x.length; i++) {
document.write(x[i].childNodes[0].nodeValue);
}
But currently I am only able to fetch one image.
How can I fetch all images instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 PHP 是一个选项,您是否查看过 SimpleXML ?
在你的情况下:
显然上面的输出不是你想要的,但是你可以根据上下文输出它。
If PHP is an option, have you looked at SimpleXML yet?
In your case:
Obviously the output above isn't what you want, but you could output it however you want depending on the context.
循环遍历
xmlDoc.getElementsByTagName('photo')
返回的所有元素。Loop through all the elements returned by
xmlDoc.getElementsByTagName('photo')
.这一行:
获取第一张照片。然后你用它做事。
将其更改为 for 循环,就像循环属性一样。
This line:
Gets the first photo. Then you do things with it.
Change that to a for loop like the one you have for looping over the attributes.