Vigilante

Vigliant Media Framework

Documentation

Scripting with Vigilante

Extending the xml class

The purpose of Vigilante's xml class is to parse XML data for display on a web page.

A significant portion of the class happens in the end handler function, the function triggered when PHP's expat parser encounters a closing XML tag. To allow for customization, the end handler function is not included in the xml class. Instead, you must extend the class and provide your own. Vigilante includes an extension to parse RSS files, which you can use as a model for your own extensions.

TIP Save your extensions in the /includes/custom/ folder, and call them through the custom.php include.

Example 3-5-1 shows the basic structure of the provided end handler.

Example 3-5-1
function endHandler($parser, $sTag)
{
//Initialize an array key.
//Make it static so the value is saved upon exit of the method.
     static $k;

//If array key is not set, initialize it to 0.
     if (!isset($k)) {$k=0;}

//Assign $sData property to a buffer.
     $sData = $this->sData;

//Assign XML data to an associative array.
//Save the XML tag name as an array key, indexed by the static numeric key.
     switch (strtoupper ($sTag))
     {
//One of the XML tags should be used to iterate the numeric key.
          case "ENDTAG01":
               $this->sOut['EndTag01'][] = $k;
               $k++;
               break;
          case "ENDTAG02":
               $this->sOut[$key]['EndTag02'] = $sData;
               break;
          [. . . more cases with end tags . . .]
     }
}

To use this end handler function in your own extensions, simply create a case statement within the switch (strtoupper($sTag)) statement for each end tag used in the XML file. Assign the property containing character data ($this->sTag) to a two-dimensional associative array with one index as an identifier, the other to store the tag name. One of the end tags will need to iterate a counter that indexes the array.

After the xml class successfully parses the file, you can use the resulting output object (in the above example, $this->sOut) to display the data.

Example 3-5-2
<?=$xml->sOut[0]["EndTag02"];?><br>
<?=$xml->sOut[1]["EndTag02"];?><br>
<?=$xml->sOut[2]["EndTag02"];?><br>