how to get xml CDATA content and write in csv format in php?


Help on PHP

<!–?php
$url = ‘http://calendar.forex-tsd.com/calendar.php?xml=1&calendar[]=4’;
//get file content but not display in browser because xml content some <![CDATA[ CDATA – (Unparsed) Character Data
$test = file_get_contents($url);
//so we replace replace with @#$ and store into $arr array variable.
$arr=explode(‘@#$’,str_replace(‘]]>’,”,str_replace(‘<![CDATA[‘,’@#$’,$test)));
$str[]=”;
//we open or create a file.csv file.
$fp = fopen(‘file.csv’, ‘w’);
//we write a data in csv file from xml data.
//if we write with $srch variable then we remove xml tags otherwise write xml tags in csv.
//create array with xml tags.
$srch=array(“</eventID><calendarID>”,”</calendarID><date>”,”</date><createdOn>”,”</createdOn><public>”,”</public><title>”,”</title><detail>”,”</detail><typeID>”,”</typeID><impact>”,”</impact><currency>”,”</currency><actual>”,”</actual><forecast>”,”</forecast><previous>”,”</forecast><previous>”,”</previous><period>”,”</period></event><event><eventID>”,”</period></event></response>”);

//replace a string in for loop and write one by one in csv file.
for($i=1,$j=1;$i<count($arr);$i++,$j++) {

$str[].=str_replace($srch,”,$arr[$i]);

// here my logic is not part of our script.

if($j==14) {

fputcsv($fp, $str);

unset($str);

$str[]=”; $j=0;

}

} fclose($fp);

?>

View original post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s