PHP Extracting CDATA from XML



So I am working with an XML data that I am getting from a site. The XML contains CDATA and my script is not picking it up.


XML



<data>
<weather>
<date>2015-01-24</date>
.
.
.
<weatherCode>116</weatherCode>
<weatherIconUrl>
<![CDATA[
http://ift.tt/1ghyyqr
]]>
</weatherIconUrl>
.
.
.
</weather>
</data>


I searched the Internets and stackoverflow. I tried the following:


1.



$xml = simplexml_load_file($strURL, 'SimpleXMLElement', LIBXML_NOCDATA);


2.



$strImage = (string)$node->weatherIconUrl;


3.



$strImage = var_dump((string)$node->weatherIconUrl);


4.



mb_convert_case(str_replace(' ', '', trim(html_entity_decode(strip_tags($item->description)))), MB_CASE_UPPER, "UTF-8");


Nothing really seems to be working. I put part of my code up. I am using a shared server that has PHP 5.2.


PHP CODE:



<?php
$strURL = "http://ift.tt/1uKjM5k";

$xml = simplexml_load_file($strURL, 'SimpleXMLElement', LIBXML_NOCDATA);
$query = "//data/weather";
$weather_nodes = $xml->xpath($query);


foreach($weather_nodes as $node) {

$strImage = $node->weatherIconUrl;
echo "Image: " . $strImage . "</br>";

}

?>

No comments:

Post a Comment