PHPBuilder - Dynamic XML Conversion Using the SAX Parser Page 2



RSS Twitter
Articles Php Functions

Dynamic XML Conversion Using the SAX Parser - Page 2

by: PHP Builder Staff
|
April 28, 2003

Thinking Up Some XML Tags

First you have to identify repeating elements of your web page. This can be menus, headlines, links, shopping cart products and so on. Then look at the parameters you want to assign to your elements. Look at this example XML, and you will get the idea:
file test.pxml:
<doc title="Pizza menu" bgcolor="lightblue">
	<bigheadline>
		Pizza Palace - Our Menu for  <dayofweek />
	</bigheadline>
	<br /><br />
	<b>Buon appetito!!!</b>
	<br /><br />
	
	<nicebox bordercolor="green">
		<product id="0" /><br />
		<product id="1" /><br />
		<product id="2" /><br />
		<product id="3" /><br />
		<product id="4" /><br />
	</nicebox>
</doc>

Dynamically Constructing XML

PHP doesn't care if it is embedded in HTML or XML. So if we use a little trick, we are able to use PHP to construct our XML.
This function creates an output buffer, opens and executes a file using the include-function, and returns the contents of the output buffer.

<?php 
function LoadAndExec($filename) {
    
ob_start();
    include(
$filename);
    
$content=ob_get_contents();
    
ob_end_clean();
    return 
$content;
}
?>
So now we can create the XML on the fly:
file test.pxml:
<doc title="Pizza menu" bgcolor="lightblue">
	<bigheadline>
		Pizza Palace - Our Menu for  <dayofweek />
	</bigheadline>
	<br /><br />
	<b>Buon appetito!!!</b>
	<br /><br />
	
	<nicebox bordercolor="green">


        <?php for($x=0;$x<5;$x++){ ?>
            <product id="<?php echo $x;?>" /><br />
        <?php ?>
</nicebox> </doc>

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Comment:



Comment:

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