我有以下XML架構:PHP 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功能,我可以正確地提取「祿」。問題在於「圖像」。我嘗試了很多測試,但都沒有。也許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文件是重要的,它是如何被構建的。 從同一個「url」部分,我必須提取「loc」和「images:images/images:loc」的細節。
我試圖與此可能的解決方案:Parse XML with Namespace using SimpleXML
,但它不與需要提取一個簡單的參數(上述)中,用節點(圖像/ LOC)的參數擬合。
可能的複製(http://stackoverflow.com/問題/ 595946 /解析的XML-與名稱空間使用,simplexml的) – har07