Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3434474
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:42:10+00:00 2026-05-18T07:42:10+00:00

I need help in creating a xml file from my php code. I created

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:42:11+00:00Added an answer on May 18, 2026 at 7:42 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need help with disk_total_space function.. i have this on my code <?php $sql=select
I need some help creating this extension method. My view inherits from <%@ Page
I want to create a xml file from the data given by the users.
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I need an automated process for creating docx files from xhtml source. The xhtml
I need help on creating an application like Visio in Silverlight, of course a
I need your help ! I want to know how do Hudson generate a
I need help understanding some C++ operator overload statements. The class is declared like
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,
I need help getting my head around the difference between my current OOP notion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.