Miscellaneous Code

  Home arrow Miscellaneous Code arrow XML MENU
MISCELLANEOUS CODE

XML MENU
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-06-06

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    [PHP5]
    XMLMENU

    [PHP5]
    DOMDocument and XSLTProcessor enabled are best, but not mandatory.

    Called this way :

    $menu = xmlmenu::getInstance (optional (string) sVersion, optional (string) sEncoding);

    see index.php for more information, and comments in classes.

    DESCRIPTION

    This package is meant to :
    - generate an XML menu
    - generate an HTML menu using the generated XML

    xmlmenu::getInstance () determines which extensions are or are not enabled, and instanciates the correct class.

    First step :
    - generate the XML. Can be done with the class, or manually (the xml files are stored in 'xml/'.
    They look like :
    text1text1_2text2
    3 methods :
    xmlmenu::defineNode () to define a new node
    xmlmenu::defineAttributes () to define attributes for a given node (can be done directly with xmlmenu::defineNode())
    xmlmenu::defineLink () to define the href link of the node

    Notice : the node are attributed an id via xml:id.

    - see the generated xml via echo $menu (xmlmenu::__toString() method)

    - save the xml file if needed via xmlmenu::xmlToFile()

    - load an xml from a file via xmlmenu::fileToXml()

    - apply an XSL to the menu to generate an HTML menu via xmlmenu::toHTML()
    The xsl needs to be created first, of course. I have created 2, they are stored in 'xsl/'.

    - save an html menu via xmlmenu::htmlToFile()



    By : malalam

    <?php
    class abstractxmlmenu {

    /**
    * protected (object) doc
    * DOMDocument object
    */
    protected $doc = null;

    /**
    * protected (object::node) root
    * DOMDocument root node
    */
    protected $root = null;

    /**
    * protected (array) aTypes
    * XML to HTML XSLT types
    */
    protected $aTypes = array ();

    /**
    * protected (string) sHtml
    * HTML menu string
    */
    protected $sHtml = '';

    /**
    * protected (string) sXml
    * XML menu string
    */
    protected $sXml = '';

    /**
    * public function __construct
    * constructor
    * @Param (string) sVersion : xml version
    * @Param (string) sEncoding : xml encoding
    */

    public function __construct ($sVersion = null, $sEncoding= null) {
    $aTypes = scandir ('xsl');
    foreach ($aTypes as $type) {
    $this -> aTypes[$type] = strtoupper (substr ($type, 0, -4));
    }
    }

    /**
    * public function defineNode
    * method defining a node
    * @Param (string) sText : text of the node
    * @Param (array) aAttr : array of attributes for the node
    * @Param (int) iId : id of the parent node
    * @Return (int) new node's id
    */
    public function defineNode ($sText, $aAttr = array (), $iId=0) {
    $newElem = $this -> doc -> createElement ('node', $sText);
    $dump = $this -> root -> getElementsByTagName('node');
    $iNewId = $dump -> length + 1;
    if($iId===0){
    $newElem = $this -> root -> appendChild ($newElem);
    }
    else {
    $parent = $this -> doc -> getElementById($iId);
    $newElem = $parent -> appendChild ($newElem);
    }
    $newElem -> setAttribute ('xml:id', $iNewId);
    if (!empty ($aAttr) && is_array ($aAttr)) {
    $this -> defineAttributes ($aAttr, $iNewId);
    }
    return $iNewId;
    }

    /**
    * public function defineLink
    * method defining a link on a given node
    * @Param (string) sLink : url of the link
    * @Param (int) iId : id of the node
    */
    public function defineLink ($sLink, $iId) {
    $elem = $this -> doc -> getElementById($iId);
    $elem -> setAttribute ('link', $sLink);
    }

    /**
    * public function defineAttributes
    * method defining attributes for a given node
    * @Param (array) aAttr : array of attributes
    * @Param (int) iId : id of the node
    */
    public function defineAttributes ($aAttr, $iId) {
    $elem = $this -> doc -> getElementById($iId);
    foreach ($aAttr as $attrName => $attrValue) {
    $elem -> setAttribute ($attrName, $attrValue);
    }
    }

    /**
    * public function __toString
    * method to return the XML of a menu
    * @return (string) iId : XML string
    */
    public function __toString () {
    return htmlentities ($this -> doc -> saveXML ());
    }

    /**
    * public function xmlToFile
    * method to save the xml to a file
    * @Param (string) sFileName :filename
    */
    public function xmlToFile ($sFileName) {
    $this -> doc -> save ('xml/'.$sFileName.'.xml');
    }

    /**
    * public function fileToXml
    * method to load an xml from a file
    * @Param (string) sFileName :filename
    */
    public function fileToXml ($sFileName) {
    $this -> doc -> load ('xml/'.$sFileName.'.xml');
    }

    /**
    * public function htmlToFile
    * method to save the html to a file.
    * cannot be done if XSLTProcessor is not enabled (see comments in the xmlmenu::toHTML () method to learn how to save the HTML file)
    * @Param (string) sFileName :filename
    */
    public function htmlToFile ($sFileName) {
    if (empty ($this -> sHtml)) {
    return false;
    }
    $fp = fopen ('html/'.$sFileName.'.html', 'w+');
    fwrite ($fp, $this -> sHtml);
    fclose ($fp);
    return true;
    }

    /**
    * public function toHTML
    * method to transform the xml to html
    * @Param (string) sType : XSL file to be used
    * @Return (string) sHtml : transformed HTML string
    */
    public function toHTML ($sType) {
    if (false === ( $type = array_search($sType, $this -> aTypes))) {
    return false;
    }
    $xsl = new XSLTProcessor ();
    $xsl -> importStyleSheet (DOMDocument::load ('xsl/'.$type));
    $this -> sHtml = $xsl -> transformToXML ($this -> doc);

    return $this -> sHtml;
    }
    }
    ?>

    Click to Download File



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap