看了几篇关于此事的SO帖子,但没有快乐。
我有这段代码:
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);
$xml = simplexml_load_string($string);
foreach ($xml->entry as $val) {
echo "RESULTS: " . $val->attributes() . "\n";
但我无法得到任何结果。 我特别感兴趣的是在这个片段中获取ID值为549592189:
<id im:id="549592189" im:bundleId="com.activision.wipeout">http://itunes.apple.com/us/app/wipeout/id549592189?mt=8&uo=2</id>
有什么建议吗?
答案 0 :(得分:0)
尝试xpath
:
$doc = new DOMDocument;
@$doc->loadHTML($string);
$xpath = new DOMXpath($doc);
$r = $xpath->query("//id/@im:id");
$id = $r->item(0)->value;
答案 1 :(得分:0)
SimpleXML使您可以轻松地深入了解XML结构并获取所需的元素。不管它是什么,都不需要正则表达式。
<?php
// Load XML
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$xml = new SimpleXMLElement($string);
// Get the entries
$entries = $xml->entry;
foreach($entries as $e){
// Get each entriy's id
$id = $e->id;
// Get the attributes
// ID is in the "im" namespace
$attr = $id->attributes('im', TRUE);
// echo id
echo $attr['id'].'<br/>';
}
答案 2 :(得分:0)
尝试:
$sxml = new SimpleXMLElement($url);
for($i = 0;$i <=10;$i++){
$appid= $sxml->entry[$i]->id->attributes("im",TRUE);
echo $appid;
}