FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Creating XML files using PHP





moejoe
Hi,

I need a way to dynamically create an XML file that is saved to my server as an XML.

So basically i need a way to use php to dynamically change the value of the strings, but then save it is an xml in another directory.

Thanks.
SlowWalkere
Are you reading an old xml file, changing it, and then creating a new one?

Or taking other input (i.e. DB or form input) and putting that into an xml file?

If all you have to do is write the xml file, it's pretty simple. Store up the information you want to write in a string, use fopen() to open a stream to the xml file, use fwrite to put the string contents into the file, and then fclose() the file.

If you need to read an old xml file, it's a bit more complicated. PHP comes with some XML parsing functions.

I just finished a simple project along those lines today. I found a pretty good tutorial at TechRepublic.com. Reading through that tutorial, you should get a handle on how to parse and use the information from the old xml file.

For reference, here's the source of the file I just created today. It opens up two RSS feeds that I have elsewhere on my site, pulls out the guid and pubDate elements, and uses those to create a static sitemap file in the root directory.

The parsing functions I got pretty much straight from that tutorial, so look there if your confused about what's going on.

Good luck,
- Walkere

Code:
/********************************************
  extra_sitemap.php
 
  This script takes RSS feeds, defined in an array
  at the beginning of the file, and compiles them
  into a Sitemap for Google.
 
  The feeds are all of database-driven pages that
  don't appear in the usual sitemap, so they
  need to be added here

*********************************************/
    ini_set("display_errors", "on");
     $GLOBALS['guid'] = false;
     $GLOBALS['pubDate'] = false;
     $GLOBALS['tagText'] = array();
     $GLOBALS['tempText'] = NULL;
     
  // function: startElement
  // Deals with the starting element
  function startElement( $parser, $tagName, $attrs ) {
    switch($tagName)
    {
       case 'GUID':
          $GLOBALS['guid'] = true;
          break;
       
       case 'PUBDATE':
          $GLOBALS['pubDate'] = true;
          break;
       
       default: break;
    }
  }

  // function: endElement
  // Deals with the ending element
  function endElement( $parser, $tagName )
  {
   switch( $tagName )
     {
      case 'GUID':
              $GLOBALS['guid'] = false;
              $GLOBALS['gid_text'] = "";
           break;

      case 'PUBDATE':
              $GLOBALS['pubDate'] = false;
         $GLOBALS['pubDate'] = "";
              break;
             
      default:
         break;
   }
  }

  // function: charElement
  // Deals with the text in between tags
  function charElement( $parser, $text ) {
   if ($GLOBALS['guid'])
   {
      $guid_text = htmlspecialchars(trim($text));
      $GLOBALS['tempText'] = $guid_text;
   }
   else if ($GLOBALS['pubDate'])
   {
      $pubDate_text = htmlspecialchars(trim($text));
      $GLOBALS['tempText'] .= "\t" . $pubDate_text;
      $GLOBALS['tagText'][] = $GLOBALS['tempText'];
      $GLOBALS['tempText'] = NULL;
   }
   
  }

   //  Start the main file/function
   //  This needs to be fully defined URLs.  Relatives don't work.
   $feed_urls = array();
   
   //  Remote Settings
   $feed_urls[] = "http://www.teachbabel.com/index.php?page=quest-feed&nolimit=true";
   $feed_urls[] = "http://www.teachbabel.com/index.php?page=plans-feed&nolimit=true";
   
   $sitemap_url = "http://www.teachbabel.com/extra_sitemap.xml";
   $sitemap_loc = "/hsphere/local/home/walkere/teachbabel.com/extra_sitemap.xml";
      
   /*  Local Settings
   $feed_urls[] = "http://127.0.0.1/teach/index.php?page=quest-feed&nolimit=true";
   $feed_urls[] = "http://127.0.0.1/teach/index.php?page=plans-feed&nolimit=true";

   $sitemap_url = "http://127.0.0.1/teach/extra_sitemap.xml";
   $sitemap_loc = "/home/walkere/www/teach/extra_sitemap.xml";
   */
   
   $changefreq = 'monthly';
   $priority = '0.3';
   
/*   //  Create the parser to handle the XML data in the feeds
   $xmlParser = xml_parser_create();
   
   //  Sets up the handler, using the functions we created
   xml_set_element_handler( $xmlParser, "startElement", "endElement" );
   
   //  Set up the character handler, using the function above
   xml_set_character_data_handler( $xmlParser, "charElement" );*/
   
   foreach ($feed_urls as $feed)
   {
      //  Create the parser to handle the XML data in the feeds
      $xmlParser = xml_parser_create();
   
      //  Sets up the handler, using the functions we created
      xml_set_element_handler( $xmlParser, "startElement", "endElement" );
   
      //  Set up the character handler, using the function above
      xml_set_character_data_handler( $xmlParser, "charElement" );

      $fp = fopen($feed, "r") or die ("Could not open RSS feed.");
   
      while($data = fread($fp, 4096))
      {
         xml_parse($xmlParser, $data, feof($fp));
      }
   
        // Close file open handler
       fclose($fp);
       
       //  Free the parser from memory
      xml_parser_free($xmlParser);
   }

   $header = "<?xml version='1.0' encoding='UTF-8' ?>
<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'>
";
   $body = array();
   foreach ($GLOBALS['tagText'] as $tagText)
   {
      $temp = explode("\t", $tagText);
      $loc = $temp[0];
      $lastmod = $temp[1];
      $lastmod = strtotime($lastmod);
      $lastmod = date("Y-m-d", $lastmod);
      
//  Output the whole url tag for the xml
   $body[] = "
<url>
  <loc>$loc</loc>
  <lastmod>$lastmod</lastmod>
  <changefreq>$changefreq</changefreq>
  <priority>$priority</priority>
</url>
";
   }
      
   $footer = "
</urlset>
";
   $fp = fopen($sitemap_loc, 'w');   
   
   fwrite($fp, $header);
      
   foreach ($body as $para)
      fwrite($fp, $para);
      
   fwrite($fp, $footer);
   
   fclose($fp);
   
   header("Location: $sitemap_url");
moejoe
Thanks!

I'm creating a new one in a new directory.

I got one to create using this method:

Code:

$FileName = "xml.xml";
$fh = fopen($FileName, 'w') or die("can't open file");
fwrite($fh, "XML");
fclose($fh);


And thats working nicly!
Thanks for your help.
manav
i came across something at pear...

just try and google it.... its a pear module to do what u want to...
Related topics
[tutor] How to protect images without htaccess using PHP
PHP without mySQL
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
how to compress a file using php ?
An Easier Way To Search An Array? Need Help Please.
Web developer
Hot to change "Reset Ownership" using php?
Using php to change content of a .txt
Access Java 1.6 Web service using php client
"Drop Down List Box" Using PHP and MySql
Parsing XML Data using Regular Expressions
Fetch Alexa Rank of website using PHP function easily
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.