PHPBuilder - Using PHP and XML with Apache Cocoon Page 3



RSS Twitter
Articles Php Functions

Using PHP and XML with Apache Cocoon - Page 3

by: Darren Beale
|
July 30, 2000

Stage 2) Bring on the PHP

Ok, so this is all well and good, but It’s static mark-up, I want to use PHP to generate the XML dynamically, so lets look at the next example, phpbuilder.php:

<?php 

/*
    I want to print out a greeting to a number of 
    countries, not just the World, so in no particular order
*/

$countries = array (
    
"UK""USA""France""Germany""Holland",
    
"Belgium""Spain""Denmark""Finland"
    
"Sweden""Japan""China""New Zealand""Australia"
);

//Now open up a file on the server called dynamic-phpbuilder.xml

$handle fopen("./dynamic-phpbuilder.xml","w");

/*
    Now we build up a string that we can save out to our file, notice that the 
    "<?" tags have been separated so we don't confuse the PHP interpreter.
*/

$tofile "<" "?" "xml version=\"1.0\"" "?" ">\n";
$tofile .= "<" "?" "xml-stylesheet href=\"dynamic-phpbuilder.xsl\" type=\"text/xsl\"" "?" ">\n";
$tofile .= "<" "?" "cocoon-process type=\"xslt\"" "?" ">\n\n";
$tofile .= "<phpbuilder>\n";
$tofile .= "\t<heading>This is a Dynamically Generated XML that has been Transformed using XSLT</heading>\n";

/*
    We now do a very simple loop and output our 
    greeting to all the countries in our example array:
*/

for ($x=0;$x<sizeof($countries);$x++) {
    
$tofile .= "\t<message>\n";
    
$tofile .= "\t\t<greeting>" $countries[$x] . "</greeting>\n";
    
$tofile .= "\t</message>\n";
}

$tofile .= "</phpbuilder>\n";

//Write the string to our file and close the handle

fwrite($handle,$tofile);
fclose($handle);

//Finally we redirect to the newly created dynamic-phpbuilder.xml file.

header("Location: dynamic-phpbuilder.xml");

?>

« Previous Page
1
|
2
|
3
|
4
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Darren Beale

Comment:



Comment:

(Maximum characters: 1200). You have characters left.