dongqiang2069 2013-06-06 16:30
浏览 33
已采纳

xml echo子节点循环

I have an xml file with child node with the same name like this:

    <element id="13" size="normal" category="blog" url="Formlabs-3D-printer">
        <date>April 12, 2013</date>
        <tag tag="printer printing 3d formlab kickstarter"></tag>
        <urlpage url="single.php?blog_no=12"></urlpage>
        <urlimage src='./Post thumbnail images/aisa_big.png'></urlimage>
        <sliderimages> 
            <image src='./Post thumbnail images/aisa_big.png'></image>
            <image src='./Post thumbnail images/01.png'></image>
            <image src='./Post thumbnail images/tape-dribbble.jpg'></image>
            <image src='./Post thumbnail images/pissed_pickle.png'></image>
        </sliderimages>
    </element>

To read my xml file I do this actually :

<?php
    if (isset($_GET["page"])) { 

    $i = $_GET["page"];

    $xml = simplexml_load_file('data.xml');

    $element = $xml->xpath(" //element[@url='$i'] ")[0];
    } 
?> 

With this code I echo some node of desired <element>. It works great!

I want to echo each image node inside <sliderimages>. But I have some problem to loop inside it...

Here my code for the loop (EDIT):

<?php
foreach($element->sliderimages as $sliderimages) {
    echo "<div class='Slide'><img src='";
    echo $sliderimages->image['src'];
    echo "'/></div>";
}
?>

With this code it echo only the first child node ....

Sorry for my English, I'm French.

  • 写回答

1条回答 默认 最新

  • dongshanya2008 2013-06-06 17:22
    关注

    You are looping over <sliderimages> and there is only one in your input XML.

    Try (not tested):

    foreach($element->sliderimages->image  as $image) {
     ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址