[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 :
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
developerWorks - FREE Tools! |
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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! |