PHPBuilder - Dynamic XML Conversion Using the SAX Parser Page 3



RSS Twitter
Articles Php Functions

Dynamic XML Conversion Using the SAX Parser - Page 3

by: PHP Builder Staff
|
April 28, 2003

Creating the conversion functions

Each conversion function has two arguments:
  1. $contents: The character data between the opening and closing tag. This can include text, HTML tags and the output of conversion functions for tags nested between the currently processed. The function should include this in its return value.
  2. $attrs: An array containing the attributes of the tag. Note that the EXPAT parser automatically changes the attribute names to uppercase.
So the function to handle this tag: <font face="bold">blah</font> receives these attributes:

<?php
$contents
='blah';
$attrs=array('FACE'=>'bold'); 
?>
Here is an example conversion function:

<?php
function handle_bigheadline($contents,$attrs) {
    return(
'<b><font size="5" face="Verdana">'.$contents.'</font></b>');
}
The function wraps the contents it receives into some HTML to make it look like a headline. It then returns this modified content to the conversion framework, which uses it as part of the contents parameter for the function that converts the tag surrounding it.
Here is the function to convert the <dayofweek /> - tag:

<?php
function handle_dayofweek($contents,$attrs) {
    return(
date("l"));
}
?>
The function ignores the contents and attributes which are passed and just returns the name of the current weekday. So if you put some stuff between -Tags, it would never show up in the converted HTML.
Building the Conversion Framework
As mentioned before, we will use the SAX parser to build the conversion framework.
SAX is an event based parser. It works like this:
It chops up the document into elements. There are three element types: Start tags, end tags and character data. (Actually there are some more, but we won't need them in this example.)
Then for each element it encounters in the document, the parser calls a function assigned to the element type.
The element handling functions receive these parameters:
FunctionParameters
Opening TagName of the tag, array containing its attributes
Character data A string containing the characters
Closing TagName of the tag
We want to combine these three functions, so that we can use the attribute data to handle the contents data.
Our conversion functions will be called when the parser encounters a closing tag.
  1. An array containing the attributes of the tag (the SAX parser passes this to the opening tag function)
  2. The character data between the opening and closing tag (can contain the output of conversion functions for other tags nested between these tags)
We will have to think up a way to store these values when we receive them so that we have them at hand when the parser encounters the closing tag.

« 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.