我使用file_get_contents函数获取一个xml,然后使用它创建一个SimpleXMLElement。
可以在这里看到xml文件:http ://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Nirvana&api_key =0ca5b0824b7973303c361510e7dbfced
问题是我需要获取lfm-> artist-> image [@ size ='small']的值,而我却找不到方法。
您应该为此使用DOMXPath:http ://php.net/manual/en/class.domxpath.php
此XPath查询将适用于您的XML:
\\lfm\artist\image[@size='small']
如下:
$doc = new DOMDocument();
$doc->loadXML($url);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("\\lfm\artist\image[@size='small']");
if (!is_null($elements)) {
foreach ($elements as $element) {
echo "<br/>[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句