使用 Javascript 或 PHP 创建 XML 文件

发布于 2025-01-05 05:56:04 字数 758 浏览 0 评论 0原文

我有一个 XML 文件,如下所示:

<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 XML 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悍妇囚夫 2025-01-12 05:56:04

如果 PHP 是一个选项,您是否查看过 SimpleXML

在你的情况下:

    $bracelets_xml = <<<XML
    <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>
XML;
    
$bracelets = new SimpleXMLElement($bracelets_xml);
    
foreach($bracelets -> photo as $photo) {
    $counter++;
    echo "Photo " . $counter . ":\r\n";
    echo "Filename : " . $photo['filename'] . "\r\n";
    echo "Thumbnail : " . $photo['thumbnail']. "\r\n";
    echo "Description : " . $photo['description']. "\r\n";
}
    

显然上面的输出不是你想要的,但是你可以根据上下文输出它。

If PHP is an option, have you looked at SimpleXML yet?

In your case:

    $bracelets_xml = <<<XML
    <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>
XML;
    
$bracelets = new SimpleXMLElement($bracelets_xml);
    
foreach($bracelets -> photo as $photo) {
    $counter++;
    echo "Photo " . $counter . ":\r\n";
    echo "Filename : " . $photo['filename'] . "\r\n";
    echo "Thumbnail : " . $photo['thumbnail']. "\r\n";
    echo "Description : " . $photo['description']. "\r\n";
}
    

Obviously the output above isn't what you want, but you could output it however you want depending on the context.

夏天碎花小短裙 2025-01-12 05:56:04

循环遍历 xmlDoc.getElementsByTagName('photo') 返回的所有元素。

Loop through all the elements returned by xmlDoc.getElementsByTagName('photo').

久夏青 2025-01-12 05:56:04

这一行:

x=xmlDoc.getElementsByTagName('photo')[0].attributes; 

获取第一张照片。然后你用它做事。

将其更改为 for 循环,就像循环属性一样。

This line:

x=xmlDoc.getElementsByTagName('photo')[0].attributes; 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文