kannst du die beiden Files mal mit folgenden austauschen und schauen, ob es bei dir funktioniert?
Ich bekomme mein PHP nicht dazu, die Warnungen auszusprucken..
class.xmlparser.php:
Code: Alles auswählen
<?php
/*****************************************
*
* $Id: class.xmlparser.php,v 1.5 2005/05/19 13:54:56 timo.hummel Exp $
*
* File : $RCSfile: class.xmlparser.php,v $
* Project :
* Descr :
*
* Author : $Author: timo.hummel $
* Modified : $Date: 2005/05/19 13:54:56 $
*
* © four for business AG, www.4fb.de
******************************************/
/**
* Class for parsing XML documents using SAX
*
* This class is a abstraction class for the PHP Expat XML functions.
*
* You can define handler functions/objects for start, end, PI and data sections (1.) or
* your can define path which will trigger the defined event when encountered (2.)
*
* Example:
*
* 1.) $parser->setEvents(array("startElement"=> "myFunction",
* "endElement"=> "myFunction",
* "characterData"=> "myFunction",
* "processingInstruction" => "myFunction");
*
* The value can also be an array with the object reference and the method to call.
* i.e. "startElement"=>array(&$myObj, "myMethod") instead of "startelement"=>"myFunction"
*
* 2.) $parser->setEvents(array("/root/foo/bar"=>"myFunction"));
*
* Valid array keys are: 'startElement', 'endElement', 'characterData', 'processingInstruction' and paths
* folowing the scheme '/root/element'. The path MUST begin from the root element and MUST start with '/'.
*
* The value can also be an array with the object reference and the method to call.
* i.e. "/foo/bar"=>array(&$myObj, "myMethod") instead of "/foo/bar"=>"myFunction"
*
* It has 3 public methods:
*
* setEventHandlers - Set specific handlers for the xml parser
* parseFile - Used to parse a XML file
* parse - Used to parse a XML string
*
* A small example:
*
* include ("class.xmlparser.php");
*
* // The XML String
* $xml = '
* <?xml version="1.0"?>
* <foo>
* <bar>some text</bar>
* <bar>another text</bar>
* </foo>';
*
* function myHandler($name, $attribs, $content)
* {
* echo "<b style='color:red'>HIT</b>: [ <b>$name</b> ] [ $content ]<br/>";
* }
*
* $parser = new XmlParser; // Parser instance
* $parser->setEventHandlers(array("/foo/bar"=>"myHandler")); // Define our handler
* $parser->parse($xml); // Parse the XML string
*
* Report bugs to: jan.lengowski@4fb.de
*
* @author Jan Lengowski <Jan.Lengowski@4fb.de>
* @copyright four for business AG <www.4fb.de>
* @version 1.0
* @package 4fb_XML
*/
class XmlParser
{
/**
* XML Parser autofree
*
* @var bool
*/
var $autofree = true;
/**
* XML Parser object
*
* @var object
* @access private
*/
var $parser;
/**
* Error message
*
* @var string
* @access private
*/
var $error = '';
/**
* Element depth
* @var int
* @access private
*/
var $depth = -1;
/**
* Element counter
* @var int
* @access private
*/
var $count = 0;
/**
* Path counter
* @var int
* @access private
*/
var $pcount = 0;
/**
* Array for creating the path
* @var array
* @access private
*/
var $paths = array();
/**
* Data storage container for the path data
* @var array
* @access private
*/
var $pathdata = array();
/**
* String storing the active path
* @var string
* @access private
*/
var $activepath = '';
/**
* The active node
* @var string
* @access private
*/
var $activenode = '';
/**
* The defined events
* @var array
* @access private
*/
var $events = array();
/**
* Construtor function
*
* @access private
* @return void
*/
function XmlParser()
{
$this->_init();
}
/**
* Initialize the XML Parser object and sets all options
*
* @return void
* @access private
*/
function _init()
{
// Create parser instance
$this->parser = xml_parser_create();
// Set parser options
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, '_startElement', '_endElement');
xml_set_character_data_handler($this->parser, '_characterData');
xml_set_processing_instruction_handler($this->parser, '_processingInstruction');
// Misc stuff
$this->events['paths'] = array(NULL);
}
/**
* Returns the XML error message
*
* @return string XML Error message
* @access private
*/
function _error()
{
$this->error = "XML error: ".xml_error_string(xml_get_error_code($this->parser))." at line ".xml_get_current_line_number($this->parser);
return $this->error;
}
/**
* Define events for the XML parser
*
* You can define handler functions/objects for start, end, PI and data sections (1.) or
* your can define path which will trigger the defined event when encountered (2.)
*
* Example:
*
* 1.) $parser->setEvents(array("startElement" => "myFunction",
* "endElement" => "myFunction",
* "characterData" => "myFunction",
* "processingInstruction" => "myFunction");
*
* The value can also be an array with the object reference and the method to call.
* i.e. "startElement"=>array(&$myObj, "myMethod") instead of "startelement"=>"myFunction"
*
* 2.) $parser->setEvents(array("/root/foo/bar"=>"myFunction"));
*
* Valid array keys are: 'startElement', 'endElement', 'characterData', 'processingInstruction' and paths
* folowing the scheme '/root/element'. The path MUST begin from the root element and MUST start with '/'.
*
* The value can also be an array with the object reference and the method to call.
* i.e. "/foo/bar"=>array(&$myObj, "myMethod") instead of "/foo/bar"=>"myFunction"
*
* @param array Options array, valid keys are 'startElement', 'endElement', 'characterData', 'processingInstruction', or a path
*
* @access public
* @return void
*/
function setEventHandlers($options = array(NULL))
{
$options = $this->_changeKeyCase($options);
if (array_key_exists('startelement', $options))
{
$this->events['startelement'] = $options['startelement'];
}
if (array_key_exists('endelement', $options))
{
$this->events['endelement'] = $options['endelement'];
}
if (array_key_exists('characterdata', $options))
{
$this->events['characterdata'] = $options['characterdata'];
}
if (array_key_exists('processinginstruction', $options))
{
$this->events['processinginstruction'] = $options['processinginstruction'];
}
$paths = $this->_getDefinedPaths($options);
$this->events['paths'] = $paths;
}
/**
* Set the processing instruction handler
*
* @param string Processing instruction handler
*
* @return void
* @access private
*/
function _processingInstruction($parser, $target, $data)
{
$handler = $this->_getEventHandler('processinginstruction');
if ($handler)
{
if (is_array($handler))
{
$handler[0]->$handler[1]($target, $data);
} else
{
$handler($target, $data);
}
}
}
/**
* Change all array keys to lowercase
* (PHP function change_key_case is available at PHP 4.2 +)
*
* @param array Source array
*
* @return array Array with lowercased keys
* @access private
*/
function _changeKeyCase($options = array())
{
$tmp = array();
foreach ($options as $key => $value)
{
$tmp[strtolower($key)] = $value;
}
return $tmp;
}
/**
* Returns events handlers if set
*
* @param string Event type
*
* @return sring Event handler name
* @access private
*/
function _getEventHandler($event)
{
// Standard events
if (array_key_exists($event, $this->events))
{
return $this->events[$event];
}
// Paths events
if (array_key_exists($event, $this->events['paths']))
{
return $this->events['paths'][$event];
}
// No events found
return false;
}
/**
* Return all defined paths from the options
* array and returns a new array containing
* only the paths to function bindings
*
* @param array Options array
*
* @return array Paths array
* @access private
*/
function _getDefinedPaths($options)
{
$tmp = array();
foreach ($options as $key => $value)
{
if (strstr($key, '/'))
{
$tmp[$key] = $value;
}
}
return $tmp;
}
/**
* Add a path to the stack
*
* @param string Element node name
* @access private
* @return void
*/
function _addPath($depth, $name)
{
$this->paths[$depth] = $name;
}
/**
* Returns the current active path
*
* @access private
*/
function _getActivePath()
{
$tmp = array();
for ($i=0; $i<=$this->depth; $i++)
{
$tmp[] = $this->paths[$i];
}
$path = '/' . join('/', $tmp);
return $path;
}
/**
* XML start element handler
*
* @param resource XML Parser resource
* @param string XML Element node name
* @param array XML Element node attributes
*
* @return void
* @access private
*/
function _startElement($parser, $name, $attribs)
{
// Increase depth
$this->depth ++;
// Set active node
$this->activenode = $name;
// Increase element counter
if ($this->activenode == $this->pathdata[$this->activepath][$this->count]['name'])
{
$this->count ++;
} else
{
$this->count = 0;
}
// Entering element context, add subpath
$this->_addPath($this->depth, $name);
// Get the handler for this event
$handler = $this->_getEventHandler('startelement');
// If a handler is defined call it
if ($handler)
{
if (is_array($handler))
{
$handler[0]->$handler[1]($name, $attribs);
} else
{
$handler($name, $attribs);
}
}
// Check for defined path handlers
$this->activepath = $this->_getActivePath();
// Save path data
$this->pathdata[$this->activepath][$this->count] = array('name'=>$name, 'attribs'=>$attribs);
}
/**
* XML character data handler
*
* @param resource XML Parser resource
* @param string XML node data
*
* @return void
* @access private
*/
function _characterData($parser, $data)
{
// Reset node count
if ($this->activenode != $this->pathdata[$this->activepath][$this->count]['name'])
{
$this->count = 0;
}
// Save path data
$this->pathdata[$this->activepath][$this->count]['content'] .= $data;
// Get the handler for this event
$handler = $this->_getEventHandler('characterdata');
// If a handler is defined call it
if ($handler)
{
if (is_array($handler))
{
$handler[0]->$handler[1]($data);
} else
{
$handler($data);
}
}
}
/**
* XML end element handler
*
* @param resource XML Parser resource
* @param string XML Element node name
*
* @return void
* @access private
*/
function _endElement($parser, $name)
{
// Get the handler for this event
$handler = $this->_getEventHandler('endelement');
// Call Element handler
if ($handler)
{
if (is_array($handler))
{
$handler[0]->$handler[1]($name);
} else
{
$handler($name);
}
}
// Reset the active path
$this->activepath = $this->_getActivePath();
// Get handler for the active path
$handler = $this->_getEventHandler($this->activepath);
// Call path handler
if ($handler)
{
if (is_array($handler))
{ // Handler is an object method
$handler[0]->$handler[1]($this->pathdata[$this->activepath][$this->count]['name'],
$this->pathdata[$this->activepath][$this->count]['attribs'],
$this->pathdata[$this->activepath][$this->count]['content']);
} else
{ // Handler is a function
$handler($this->pathdata[$this->activepath][$this->count]['name'],
$this->pathdata[$this->activepath][$this->count]['attribs'],
$this->pathdata[$this->activepath][$this->count]['content']);
}
}
// Decrease depth
$this->depth --;
}
/**
* Parse a XML string
*
* @param string XML data
*
* @return bool
* @access public
*/
function parse($data, $final = false)
{
$success = xml_parse($this->parser, trim($data), $final);
if ($final && $this->autofree)
{
xml_parser_free($this->parser);
}
if (!$success)
{
return $this->_error();
}
return $success;
}
/**
* Parse a XML file
*
* @param string File location
*
* @return bool
* @access public
*/
function parseFile($file)
{
if (!($fp = fopen($file, "rb")))
{
}
while ($sData = fread($fp, 4096))
{
if (!xml_parse($this->parser, $sData, feof($fp)))
{
$this->_error();
return false;
}
}
if ($this->autofree)
{
xml_parser_free($this->parser);
}
return true;
}
} // XML_Parser
?>
class.xmltree.php
Code: Alles auswählen
<?php
/**
* XmlTree class
*
* Class to create XML tree structures from
* scratch without the need for a XML DOM
*
* Example:
*
* $tree = new XmlTree('1.0', 'ISO-8859-1');
* $root =& $tree->addRoot('rootname', 'some content', array('foo'=>'bar'));
*
* This genererates following XML:
*
* <?xml version="1.0" encoding="ISO-8859-1"?>
* <rootname foo="bar">some content</rootname>
*
* $root now references the 'rootname' node object.
* To append childNodes use the appendChild method.
*
* $foo =& $root->appendChild('foo', 'bar');
*
* Note: From version 1.1 you can use the $obj->add() method
* as shortcut to appendchild
*
* <?xml version="1.0" encoding="ISO-8859-1"?>
* <rootname foo="bar">some content<foo>bar</foo></rootname>
*
* !! ALWAYS use '=&' with the addRoot and appendChild methods. !!
*
* @author Jan Lengowski <Jan.Lengowski@4fb.de>
* @copyright four for business AG 2003
*
* @package 4fb_XML
* @version 1.1
*/
class XmlTree
{
/**
* XML Version string
* @var string
* @access private
*/
var $_strXmlVersion;
/**
* XML Encoding string
* @var string
* @access private
*/
var $_strXmlEncoding;
/**
* Root element name
* @var string
* @access private
*/
var $_strRootName;
/**
* Root content
* @var string
* @access private
*/
var $_strRootContent;
/**
* Root attributes
* @var array
* @access private
*/
var $_strRootAttribs;
/**
* Root node
* @var object
* @access private
*/
var $_objRoot;
/**
* Tree XML string
* @var string
* @access private
*/
var $_strXml;
/**
* Indent character
* @var string
* @access private
*/
var $_indentChar = "";
/**
* Constructor
*
* @param string XML Version i.e. "1.0"
* @param string XML Encoding i.e. "UTF-8"
*
* @return void
*/
function XmlTree($strXmlVersion = '1.0', $strXmlEncoding = 'UTF-8')
{
$this->_strXmlVersion = 'version="'.$strXmlVersion.'"';
$this->_strXmlEncoding = 'encoding="'.$strXmlEncoding.'"';
}
/**
* Add a Root element to the XML Tree
*
* @param string XML Node Name
* @param string XML Node Content
* @param array Attributes array('name'=>'value')
*
* @return object Reference to the root node object
*/
function &addRoot($strNodeName, $strNodeContent = '', $arrNodeAttribs = array())
{
if (!$strNodeName)
{
return 'XmlTree::addRoot() -> No node name specified';
}
$this->_objRoot = new XmlNode($strNodeName, $strNodeContent, $arrNodeAttribs);
return $this->_objRoot;
}
/**
* Print or Return Tree XML
*
* @param boolean Return content
*
* @return string Tree XML
*/
function dump($bolReturn = false)
{
if (!is_object($this->_objRoot))
{
return 'XmlTree::dump() -> There is no root node';
}
$this->_objRoot->setIndent($this->_indentChar);
$this->_strXml = sprintf("<?xml %s %s?>\n", $this->_strXmlVersion, $this->_strXmlEncoding);
$this->_strXml .= $this->_objRoot->toXml();
if ($bolReturn)
{
return $this->_strXml;
}
echo $this->_strXml;
}
/**
* Set the indent string
* @param int level
* @return void
*/
function setIndent($string)
{
$this->_indentChar = $string;
}
} // XmlTree
/**
* XmlNode Object
*
* Object of a XML Tree Node
*
* @see XmlTree
*
* !! ALWAYS use '=&' with the addRoot and appendChild methods. !!
*
* @author Jan Lengowski <Jan.Lengowski@4fb.de>
* @copyright four for business AG 2003
*
* @package 4fb_XML
* @version 1.1
*/
class XmlNode
{
/**
* Indenting character
* @var string
*/
var $_indentChar;
/**
* Node name
* @var string
* @access private
*/
var $_strNodeName;
/**
* Node content
* @var string
* @access private
*/
var $_strNodeContent;
/**
* Added content
* @var string
* @access private
*/
var $_strNodeContentAdded;
/**
* Node attributes
* @var array
* @access private
*/
var $_arrNodeAttribs;
/**
* Enclose node content in a cdata section
* @var boolean
* @access private
*/
var $_cdata;
/**
* XML for this node
* @var string
* @access private
*/
var $_strXml;
/**
* Child count
* @var int
* @access private
*/
var $_intChildCount = 0;
/**
* Parent Node
* @var object
*/
var $parentNode = 0;
/**
* Childnodes
* @var array
* @access private
*/
var $childNodes = array();
/**
* Class Constructor
*
* @param string XML Node Name
* @param string XML Node Content
* @param array Attributes array('name'=>'value')
*
* @return void
*/
function XmlNode($strNodeName, $strNodeContent = '', $arrNodeAttribs = array(), $cdata = false)
{
if (!$strNodeName)
{
return $this->_throwError($this, '%s::Construtctor() : No node name specified.');
}
$this->_cdata = $cdata;
$this->setNodeName($strNodeName);
$this->setNodeContent($strNodeContent);
$this->setNodeAttribs($arrNodeAttribs);
}
/**
* Set a node name
*
* @param string Node name
*
* @return void
*/
function setNodeName($strNodeName)
{
$this->_strNodeName = $strNodeName;
}
/**
* Set node content
*
* @param string Node content
*
* @return void
*/
function setNodeContent($strNodeContent)
{
$this->_strNodeContent = $strNodeContent;
}
/**
* Set the node attributes
*
* @param array Node attributes array('name'=>'value')
*
* @return void
*/
function setNodeAttribs($arrNodeAttribs)
{
$this->_arrNodeAttribs = $arrNodeAttribs;
}
/**
* Set the node parent
*
* @param object Reference to the parent object
*
* @return void
*/
function setNodeParent(&$objParent)
{
$this->parentNode =& $objParent;
}
/**
* Add content to the node
*
* @param string Content
*
* @return void
*/
function addNodeContent($strNodeContent)
{
$this->_strNodeContentAdded = $strNodeContent;
}
/**
* Check if the node has childs
*
* @return boolean
*/
function hasChilds()
{
return ($this->_intChildCount > 0) ? true : false;
}
/**
* Add a child child node
*
* @param string XML Node Name
* @param string XML Node Content
* @param array Attributes array('name'=>'value')
* @param bool CDATA Section
*
* @return object Reference to the new node object
*/
function &appendChild($strNodeName, $strNodeContent = '', $arrNodeAttribs = array(), $cdata = false)
{
if (!$strNodeName)
{
return $this->_throwError($this, '%s::appendChild() : No node name specified');
}
$pos = $this->_intChildCount ++;
$this->childNodes[$pos] =& new XmlNode($strNodeName, $strNodeContent, $arrNodeAttribs, $cdata);
$this->childNodes[$pos]->setNodeParent($this);
return $this->childNodes[$pos];
}
/**
* Short for appendChild method
*
* @see appendChild
*/
function &add($strNodeName, $strNodeContent = '', $arrNodeAttribs = array(), $cdata = false)
{
return $this->appendChild($strNodeName, $strNodeContent , $arrNodeAttribs , $cdata);
}
/**
* Builds the XML string for the node using the
* node properties
*
* @return string XML String
* @access private
*/
function toXml($indent = 0)
{
// Indent for nodes markub
$sp = $this->_getIndent($indent);
// Indent for content
$csp = $this->_getIndent($indent ++);
// Increment indent
//$indent ++;
$this->_strXml = "$sp<".$this->_strNodeName.$this->_parseAttributes($this->_arrNodeAttribs);
$maxNodes = count($this->childNodes);
if ($this->_strNodeContent != '' || $this->_strNodeContentAdded != '' || $maxNodes > 0)
{
$this->_strXml .= ">";
if ($this->_cdata)
{
$this->_strXml .= "$csp<![CDATA[ ";
$content = $this->_strNodeContent . $this->_strNodeContentAdded;
$this->_strXml .= $content;
} else {
$content = $this->_strNodeContent . $this->_strNodeContentAdded;
$this->_strXml .= ($content != "") ? "$content" : $content;
}
if ($this->_cdata)
{
$this->_strXml .= " ]]>";
}
for ($i=0; $i<$maxNodes; $i++)
{
$this->childNodes[$i]->setIndent($this->_indentChar);
$this->_strXml .= $this->childNodes[$i]->toXml($indent);
}
$this->_strXml .= "$sp</" . $this->_strNodeName . ">\n";
} else
{
$this->_strXml .= "/>\n";
}
return $this->_strXml;
}
/**
* Builds a string from the attributes array
*
* @param array Attributes array('name'=>'value')
*
* @return string Attribute string
* @access private
*/
function _parseAttributes($arrAttributes = array())
{
$strAttributes = '';
if (is_array($arrAttributes))
{
foreach ($arrAttributes as $name => $value)
{
$strAttributes .= ' '.$name.'="'.$value.'"';
}
}
return $strAttributes;
}
/**
* Get indent string
* @param int level
* @return string indent string
*/
function _getIndent($level)
{
return $this->_strXml .= str_repeat($this->_indentChar, $level);
}
/**
* Set the indent string
* @param int level
* @return void
*/
function setIndent($string = "")
{
$this->_indentChar = $string;
}
} // XmlNode
?>