首页 > 解决方案 > 用php循环遍历xml,无法在同一个数组中获取图像:标题和url

问题描述

我只是想遍历这个 xml 并回显每个我已经能够单独执行的对象,但希望每个对象都在 foreach 中

$xml = new SimpleXMLElement($response);
foreach ($xml->url as $url_list) 
{
$url = $url_list->loc; 
echo $url; //works
$image_title = $url_list->image:title; 
echo $image_title; ///doesn't work
//would also like to get other 4 objects lastmod,changefreq,image:loc
}





    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <url>
    <loc>https://example.com/collections/xxx</loc>
    <lastmod>2021-02-04T17:35:40-05:00</lastmod>
    <changefreq>daily</changefreq>
    <image:image>
    <image:loc>https://example.com/collections/xxx.png?v=1582857057</image:loc>
    <image:title>xxx</image:title>
    </image:image>
    </url>
    <url>
    <loc>https://example.com/collections/yyy</loc>
    <lastmod>2021-02-04T22:50:40-05:00</lastmod>
    <changefreq>daily</changefreq>
    <image:image>
    <image:loc>https://example.com/collections/yyy.png?v=1582858125</image:loc>
    <image:title>yyy</image:title>
    </image:image>
    </url>

标签: phpxml

解决方案


推荐阅读