I have the following xml
<rss>
<channel>
<item><title><![CDATA[bla]]></title><description><![CDATA[desc1]]></description><link>blah</link></item>
<item><title><![CDATA[bleh]]></title><description><![CDATA[desc2]]></description><link>blahhh</link></item>
</rss>
</channel>
I want to get all the text in “description” however it is surrounded by CDATA.I thought the following would work
$xmlurl = file_get_contents('http://anxmlfile.xml');
$xml = simplexml_load_string($xmlurl, null, LIBXML_NOCDATA);
foreach ($xml->item as $item) {
echo $item->description->innertext;
}
I was expecting the scrript to echo “desc1desc2”, however nothing is ever echo’d.
*UPDATE
Here is some full code (I am making an rss reader)
$xml = new SimpleXMLElement('http://www.skysports.com/rss/0,20514,11661,00.xml', LIBXML_NOCDATA, true);
/* For each <character> node, we echo a separate <name>. */
foreach ($xml->item as $item) {
echo (string)$item->description;
}
SimpleXML is kind of tricky, you need to explicitly typecast everything,
echo (string)$item->description;should work.
EDIT: also, what’s with the “innertext”? Cut that out. And another thing, in that XML, is the content wrapped in some root node, such as
<items>? If not, foreach won’t work.EDIT2: okay, after the edited question, it becomes clearer, you’re doing the iteration wrong, leaving out
<channel>. This will work: