父:子详细信息的PHP XML解析器
我有以下XML模式:父:子详细信息的PHP XML解析器,php,xml,xml-parsing,parent-child,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>
<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>
观察XML文件及其构建方式非常重要。
从相同的“url”部分,我必须提取“loc”和“images:images/images:loc”细节
我尝试了这个可能的解决方案:
但它不适合提取简单参数(loc)和带有节点的参数(images/loc)
“我尝试了这个可能的解决方案:
但它不适合提取简单参数(loc)和带有节点的参数(图像/loc)。”
我可能误解了这个问题,但如果您愿意,可以将这两种方法结合起来。使用xpath()
,如上链接所述,仅用于访问带前缀的元素:
//obtain the information URL->IMAGE
$image_extracted = $single_section->xpath('image:image/image:loc');
echo "IMG Extracted: " . $image_extracted[0] . "<br>";
//获取信息URL->IMAGE
$image\u extracted=$single\u section->xpath('image:image/image:loc');
回波“提取的IMG:”$图像_已提取[0]。“
”;
快速测试:可能重复的
$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;
//obtain the information URL->IMAGE
$image_extracted = $single_section->xpath('image:image/image:loc');
echo "IMG Extracted: " . $image_extracted[0] . "<br>";