这可能是xml文件
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>werwer</title>
<link>werwerwe</link>
<item>
<g:id>704667</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
<item>
<g:id>4456456</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
</channel></rss>
如何解析那个xml文件,我有脚本但它不起作用
if (file_exists('products.xml')) {
$xml = simplexml_load_file('products.xml');
print_r($xml);
} else {
exit('Failed to open products.xml.');
}
任何想法如何在g:id?
之间获取信息答案 0 :(得分:1)
您需要将命名空间应用于子项。 http://php.net/manual/en/simplexmlelement.getnamespaces.php
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>werwer</title>
<link>werwerwe</link>
<item>
<g:id>704667</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
<item>
<g:id>4456456</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
</channel>
</rss>';
$xml = simplexml_load_string($xml);
$ns = $xml->getNamespaces(true);
foreach ($xml->channel->item as $item) {
echo $item->children($ns['g'])->id.PHP_EOL;
}
/*
704667
4456456
*/