Problem in creating xml file from php -


i need in creating xml file php code. created function forms xml want it, dont know how save formed xml in file.

also if tell me, next time, how can update created xml file.

my code looks below:

public function create_xml() {        $output = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> ";     $output .= "<data>";     $output .= "<news>";      $news_articles = new c7_news();     $db = c7_bootstrap::getdb();     $foxsport_sql = "select headline, link c7_news                         source = 'foxsports' , category = 'breaking news'                         limit 0,4";     $foxsport_rowset = $db->fetchall($foxsport_sql);     $data = array();     foreach($foxsport_rowset $foxsport_row)     {         $output .= "<title>";         $output .= "<description>";         $output .= "<![cdata[";         $output .= $foxsport_row['headline'];         $output .= "]]>";         $output .= "<link>";         $output .= $foxsport_row['link'];         $output .= "</link>";         $output .= "</title>";      }     $output .= "</news>";     $output .= "</data>";  } 

i might doing wrong too, plz let me know best way create xml.

thank zeeshan

i'll add more information here.

also if tell me, next time, how can update created xml file.

you should parse xml in order updated, simplexml extension provides , easy use api parsing writing xml files.

i might doing wrong too, plz let me know best way create xml.

there many ways this, prefer use php "template engine" when writing html or xml (or *ml matter).

    <?php echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?> " ?>     <?php       $news_articles = new c7_news();       $db = c7_bootstrap::getdb();       $foxsport_sql = "select headline, link c7_news                     source = 'foxsports' , category = 'breaking news'                     limit 0,4";       $foxsport_rowset = $db->fetchall($foxsport_sql);     ?>     <data>       <news>       <?php foreach($foxsport_rowset $foxsport_row):         <title>           <description>             <![cdata[             <?php echo $foxsport_row['headline'] ?>             ]]>           </description>           <link><?php echo $foxsport_row['link']</link>         </title>       <?php endforeach; ?>       </news>     </data> 

this output xml , lot easier read

but dont know how save formed xml in file.

as how save file should have php file include "template" (suppose template called xml_template.php):

    ob_start();     include dirname(__file__) . directory_separator . 'xml_template.php';     $output = ob_get_clean(); 

then have again xml string on $output variable , can noted vlad.p:

    // if file missing, create it.     $file = fopen("file.xml", "w");     fwrite($file, $output);      fclose($file); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -