提取 从苹果饲料中提取

问题描述 投票:1回答:2

如何从下面的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>
php xml simplexml
2个回答
2
投票

您应该使用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(),valuate()也可以返回标量值。

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

0
投票

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

回答found here

© www.soinside.com 2019 - 2024. All rights reserved.