Miscellaneous Code
  Home arrow Miscellaneous Code arrow XML MENU
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! "ebook: Exploring IBM SOA Technology & Practice

    Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success.
    FREE! Go There Now!


    NEW! Discovering the value of WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Innovate don't duplicate! Asset reuse strategies for success

    Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Rational Talks to You: Grady Booch on Architecture

    Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered!
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Manual Tester V7.0.1

    Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Process

    Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly.
    FREE! Go There Now!


    NEW! Using Rational Business Developer to enhance your developer productivity

    Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components.
    FREE! Go There Now!


    NEW! Whitepaper: Achieving consistency between business process models and operational guides

    Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - 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 ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek