父XML的PHP​​ XML Parser:子详细信息

时间:2016-02-28 11:04:37

标签: php xml xml-parsing parent-child

我有以下XML架构:

<url>
   <loc>
      https://www.domain.com/artcile-title-x
   </loc>
   <mobile:mobile/>
   <image:image>
      <image:loc>
          https://www.domain.com/images/file_name.jpg
      </image:loc>
      <image:title>file_name.jpg</image:title>
   </image:image>
   <lastmod>2015-11-29T06:17:25+00:00</lastmod>
   <changefreq>monthly</changefreq>
</url>

使用simplexml_load_file函数我可以正确地提取&#34; loc&#34;。问题出在&#34; image&#34;。我尝试了很多测试,但没有。也许simplexml_load_file不正确?

例如:

//XML url location
$xmlurl = "https://www.domain.com/sitemap.xml";

//get the content in the $xmlcode var
$xmlcode = simplexml_load_file($xmlurl);

//for each "section" extracted obtain LOC and IMAGE
foreach($xmlcode->url as $single_section)
{
    //obtain the information URL->LOC
    $loc_extracted = $single_section->loc;

    echo "URL Extracted: " . $loc_extracted . "<br>";

    //obtain the information URL->IMAGE
    $image_extracted = $single_section->image->loc;

    echo "IMG Extracted: " . $image_extracted[0] . "<br>";
}

我尝试过很多测试:

$image_extracted = $single_section->image->image->loc;
or
$image_extracted = $single_section->image->image->image->loc;
or
$image_extracted = $single_section->image; //and use it as an array
or
$image_extracted = $single_section->image['image']->loc;
or
$image_extracted = $single_section->image['image']->image->loc;

观察XML文件及其构建方式非常重要。 来自同一个&#34; url&#34;我必须提取&#34; loc&#34;和&#34;图像:图像/图像:loc&#34;详情。

我尝试了这个可能的解决方案:Parse XML with Namespace using SimpleXML

但它不适合提取简单参数(loc)和带节点的参数(images / loc)。

1 个答案:

答案 0 :(得分:1)

  

&#34;我尝试了这个可能的解决方案:Parse XML with Namespace using SimpleXML

     

但它不适合提取简单参数(loc)和带节点(images / loc)的参数。&#34;

我可能误解了这个问题,但如果你愿意,你可以将这两种方法结合起来。如上面链接中所述,使用xpath()仅用于访问带前缀的元素:

//obtain the information URL->IMAGE
$image_extracted = $single_section->xpath('image:image/image:loc');

echo "IMG Extracted: " . $image_extracted[0] . "<br>";

快速测试:https://eval.in/526834