从apple feeds </im:rating>中提取<im:rating>

时间:2013-11-19 14:26:57

标签: php xml simplexml

如何从下面的xml。我使用php SimpleXmlElement来解析整个xml。我得到的除了这个元素以外的所有细节。

<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<id>
    https://itunes.apple.com/us/rss/customerreviews/id=977/sortBy=mostRecent/xml
</id>
<title>iTunes Store: Customer Reviews</title>
<updated>2013-11-19T05:55:27-07:00</updated>
<entry>
<updated>2013-11-14T05:27:00-07:00</updated>
<id>895482313</id>
<title>Still </title>
<content type="text">
The app continues to .
</content>
<im:contentType term="Application" label="Application"/>
<im:voteSum>0</im:voteSum>
<im:voteCount>0</im:voteCount>
<im:rating>4</im:rating>
<im:version>3.1</im:version>
<author>
<name>LIMHP2010</name>
<uri>https://itunes.apple.com/us/reviews/id15104323</uri>
</author>
<link rel="related" href="https://itunes.apple.com/us/review?id=722846977&type=Purple%20Software"/>
<content type="html">
<table border="0" width="100%"> <tr> <td> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr valign="top" align="left"> <td width="100%"> <b><a href="https://itunes.apple.com/us/app/tricycle-magazine/id722846977?mt=8&uo=2">Still Crashing during updating</a></b><br/> <font size="2" face="Helvetica,Arial,Geneva,Swiss,SunSans-Regular"> </font> </td> </tr> </table> </td> </tr> <tr> <td> <font size="2" face="Helvetica,Arial,Geneva,Swiss,SunSans-Regular"><br/>On iPhone 5s, iOS 7.0.3 - <br/><br/>Customer service was fantastic at fixing the problem of zooming into much. The problem of crashing while attempting to update purchases continues.<br/><br/>The app continues to be essentially useless.</font><br/> </td> </tr> </table>
</content>
</entry>
</feed>

2 个答案:

答案 0 :(得分:1)

您应该使用DOMXpath :: evaluate()。 SimpleXml隐藏了您的功能。在这种情况下,我认为您的问题是命名空间。 PHP自动注册当前上下文的命名空间(query()/evaluate()的第二个参数),你真的不想这样,它可能会干扰你自己的命名空间定义。 query()/evaluate()的第三个参数禁用自动注册。

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

// register the namespaces you would like to use with own prefixes
$xpath->registerNamespace('itunes', 'http://itunes.apple.com/rss');
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');

var_dump(
  'Entries: '.$xpath->evaluate('count(//atom:entry)', NULL, FALSE)
);
// fetch the list of entry nodes
$entries = $xpath->evaluate('//atom:entry', NULL, FALSE);
foreach ($entries as $entry) {
  // fetch values from the entry node children
  var_dump(
    $xpath->evaluate('string(atom:title)', $entry, FALSE),
    $xpath->evaluate('string(atom:content)', $entry, FALSE),
    $xpath->evaluate('string(itunes:contentType/@label)', $entry, FALSE),
    $xpath->evaluate('string(itunes:version)', $entry, FALSE),
    $xpath->evaluate('number(itunes:rating)', $entry, FALSE)
  );
}

与query()不同,evaluate()也可以返回标量值。

string(10) "Entries: 1"
string(6) "Still "
string(24) "
The app continues to .
"
string(11) "Application"
string(3) "3.1"
float(4)

答案 1 :(得分:0)

我能够做到这一点 $review->children('im', true)->rating使用SimpleXMLElement()

回答found here