Zeeshan Rang
Zeeshan Rang

Reputation: 19875

Problem in creating xml file from php

I need help in creating a xml file from my php code. I created a function which forms the xml as i want it, but i dont know how to save the formed xml in a file.

Also if you could also tell me, for next time, how can i update the already created xml file.

My code looks like 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 FROM c7_news
                        WHERE source = 'foxsports' AND category = 'Breaking News'
                        LIMIT 0,4";
    $foxsport_rowset = $db->fetchAll($foxsport_sql);
    $data = array();
    foreach($foxsport_rowset as $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 be doing something wrong too, plz let me know the best way to create the xml.

Thank you Zeeshan

Upvotes: 0

Views: 201

Answers (3)

Cups
Cups

Reputation: 6896

Use

file_put_contents( $file, $output );

You might want to pass in the argument for the path of the file:

$stream->create_xml( $file );

or reason that that should be handled by another method/class, eg

$stream->create_xml();
$stream->save_xml( $file );

Upvotes: 0

dcestari
dcestari

Reputation: 440

I'll add some more information here.

Also if you could also tell me, for next time, how can i update the already created xml file.

You should parse the XML in order to updated, the SimpleXml extension provides and easy to use API for parsing an writing XML files.

I might be doing something wrong too, plz let me know the best way to create the xml.

There are many ways to do this, but I prefer to use PHP as a "template engine" when writing HTML or XML (or any *ML for that 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 FROM c7_news
                    WHERE source = 'foxsports' AND category = 'Breaking News'
                    LIMIT 0,4";
      $foxsport_rowset = $db->fetchAll($foxsport_sql);
    ?>
    <data>
      <news>
      <?php foreach($foxsport_rowset as $foxsport_row):
        <title>
          <description>
            <![CDATA[
            <?php echo $foxsport_row['headline'] ?>
            ]]>
          </description>
          <link><?php echo $foxsport_row['link']</link>
        </title>
      <?php endforeach; ?>
      </news>
    </data>

This will output the XML and is a lot easier to read

but i dont know how to save the formed xml in a file.

As for how to save this to a file you should have another PHP file to include this "template" (suppose the template is called xml_template.php):

    ob_start();
    include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'xml_template.php';
    $output = ob_get_clean();

Then you have again the XML string on the $output variable and can do as noted by Vlad.P:

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

Upvotes: 2

Vlad.P
Vlad.P

Reputation: 1464

Add this at the end of your function.

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

Upvotes: 0

Related Questions