douji0588 2016-07-18 18:49
浏览 44
已采纳

php,xml从具有不同属性的标签输出整个值

I'm trying to make an application which outputs the entire images from an xml file. The images are stored in different img tags with different attributes . etc.. here's the xml page..

<issue>
    <coverImage id="13088328">
    0mjg7sd8q8tjk63z93p7</coverImage>
<cover>
</cover>
<page template="rowpicrowpic">
    <img id="13088334">
    wqqkrnt74l18p9nxnyf2</img>
<img id="13088364">
    fjm6cjtpxvc2t2wjcbc6</img>
</page>
<page template="pics4">
    <img id="13088373">
    nc753vx8bkt7195nz8zs</img>
<img id="13088379">
    rz7c5tkzm8mcxcfnbrch</img>
<img id="13088379">
    rz7c5tkzm8mcxcfnbrch</img>
<img id="13088373">
    nc753vx8bkt7195nz8zs</img>
</page>
<page template="rowpicrow2text">
    <txt>
</txt>
<txt>
</txt>
<img id="13098696">
    h8b6cpjmyjp344rsyzwq</img>
</page>
<page template="rowpictextrowpic">
    <txt>
        test
</txt>
<img id="13088373">
    nc753vx8bkt7195nz8zs</img>
<img id="13088334">
    wqqkrnt74l18p9nxnyf2</img>
</page>
<page template="rowtextrowpic">
    <txt>
    <![CDATA[]]>
</txt>
<img id="13085748">
    zhs2q9z92jk98yjgnhrc</img>
</page>
<page template="rowpictextrowpic">
    <txt>
    <![CDATA[]]>
</txt>
<img id="13088379">
    rz7c5tkzm8mcxcfnbrch</img>
<img id="13085727">
    nbshpr8z9mlzth9n0lt1</img>
</page>
<page template="double">
    <img id="13085697">
    vw5dnsjqs6sxvbz9z22n</img>
</page>
<page template="empty">
</page>
<page template="double">
    <img id="13098702">
    kx1hyh88jy4xdb6mqbvd</img>
</page>
<page template="empty">
</page>
<page template="onepage">
    <img id="13088400">
    159zngkbftmztzcgr013</img>
</page>
</issue>

What i'm trying to do it have all of the img values such as 0mjg7sd8q8tjk63z93p7, wqqkrnt74l18p9nxnyf2, fjm6cjtpxvc2t2wjcbc6, nc753vx8bkt7195nz8zs, rz7c5tkzm8mcxcfnbrch, and nc753vx8bkt7195nz8zs to be outputted onto a page.. I have tried to do this this way but I'm trying to do it a different way and not manually writing the path to the image.

echo "Image1: <img src='http://image-link.com/".$xmlfile->page[0]->img[1].".jpg'/>";
  • 写回答

1条回答 默认 最新

  • dounielong7728 2016-07-18 18:59
    关注

    Use simplexml object and xpath to get list of all images in xml

    $xml = simplexml_load_string($str);
    foreach($xml->xpath('//img|coverImage') as $img) 
        echo trim($img) ."
    ";
    

    demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?