這解析圖像和文字是線程這裏的延續:Trying to Parse Only the Images from an RSS Feed試圖從一個RSS源
這一次,我想從一個RSS feed解析圖像和某些項目。 RSS提要的採樣是這樣的:
<channel>
<atom:link href="http://mywebsite.com/rss" rel="self" type="application/rss+xml" />
<item>
<title>Article One</title>
<guid isPermaLink="true">http://mywebsite.com/details/e8c5106</guid>
<link>http://mywebsite.com/geturl/e8c5106</link>
<comments>http://mywebsite.com/details/e8c5106#comments</comments>
<pubDate>Wed, 09 Jan 2013 02:59:45 -0500</pubDate>
<category>Category 1</category>
<description>
<![CDATA[<div>
<img src="http://mywebsite.com/myimages/1521197-main.jpg" width="120" border="0" />
<ul><li>Poster: someone's name;</li>
<li>PostDate: Tue, 08 Jan 2013 21:49:35 -0500</li>
<li>Rating: 5</li>
<li>Summary:Lorem ipsum dolor </li></ul></div><div style="clear:both;">]]>
</description>
</item>
<item>..
下面我有,我嘗試解析圖像和文字下面的代碼:
$xml = simplexml_load_file('http://mywebsite.com/rss?t=2040&dl=1&i=1');
$descriptions = $xml->xpath('//item/description');
$mytitle= $xml->xpath('//item/title');
foreach ($descriptions as $description_node) {
// The description may not be valid XML, so use a more forgiving HTML parser mode
$description_dom = new DOMDocument();
$description_dom->loadHTML((string)$description_node);
// Switch back to SimpleXML for readability
$description_sxml = simplexml_import_dom($description_dom);
// Find all images, and extract their 'src' param
$imgs = $description_sxml->xpath('//img');
foreach($imgs as $image) {
echo "<img id=poster class=poster src={$image['src']}> {$mytitle}";
}
}
上面的代碼精美提取圖像....但是,當我嘗試在我的代碼的最後一行時,它不會提取$ mytitle(這將是「Article One」)標記。這應該是從RSS提要中的所有項目中提取的。
任何人都可以幫我找出這一個請。
非常感謝,
赫爾南
XPath是正確的。也許你需要在'$ mytitle'上調用' - > nodeValue'來獲取節點內容。 – helderdarocha
其實,因爲你有很多'item'元素,你將需要使用' - > item(0)'來獲得第一個元素。 – helderdarocha
謝謝Helderdarocha ......不幸的是,我的知識並不先進,我無法理解你的解釋。問題是我必須提取字段內的內容以及字段中的內容。這將在RSS提要中重複多次,這正是我想要的。 –
Hernandito