Using xml_parser_create() within a class and using its methods to handle xml events

Discussion in 'PHP' started by GazPoint, Mar 30, 2008.

  1. #1
    Hi,

    I am using xml_parser_create() and the associated functions to parse an xml file containing three <note> nodes, based loosely on emails. Got it working but I now want to move it all into a self contained class (called "Emails") that I instantiate with just a filename that contains the xml. The class contains the functions that handle the events taht the xml parswer raises as it traverses the xml.

    The problem is that when I call xml_set_element_handler() and xml_set_character_data_handler() I get an error to do with the methods I want to handle the xml parser events :

    Undefined property: Emails::$fromxml_startnode

    I have a construct function in my class which calls these functions like this :

    xml_set_element_handler( $xml_parser, "$this->fromxml_startnode", "$this->fromxml_endnode");
    and
    xml_set_character_data_handler( $xml_parser, "$this->fromxml_data");

    I get the feeling that the xml parser functions don't work because it has it's own scope. Am I right? Is this a limitation of PHP or am I just apporaching it wrongly?

    Thanks, hope you can help

    My test php file :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    	<title>Xml test</title>
    </head>
    
    <body>
    
    <?php
    class Email
    {
    	public $To;
    	public $From;
    	public $Title;
    	public $Body;
    
    	function __construct()
    	{
    	}
    
    	function EchoMe()
    	{
    		echo "<table>";
    		echo "<tr><td>To</td><td>$this->To</td></tr>";
    		echo "<tr><td>From</td><td>$this->From</td></tr>";
    		echo "<tr><td>Title</td><td>$this->Title</td></tr>";
    		echo "<tr><td>Body</td><td>$this->Body</td></tr>";
    		echo "</table><hr />";
    	}
    }
    
    class XmlNode
    {
    	public $NodeName;
    	public $Attributes;
    
    	function __construct()
    	{
    	}
    }
    
    class Emails
    {
    	public $Items;       //The list of email records
    	private $XmlNode;    //The current xml node we are working on
    	private $xml_parser; //
    
    	function __construct()
    	{
    	}
    
    	function construct_fromxml_file( $filename)
    	{
    		$xml_parser = xml_parser_create();
    
    		//Specify element handler
    		xml_set_element_handler( $xml_parser, "$this->fromxml_startnode", "$this->fromxml_endnode");
    
    		//Specify data handler
    		xml_set_character_data_handler( $xml_parser, "$this->fromxml_data");
    
    		//Open XML file
    		$fp = fopen( $filename, "r");
    
    		//Read data
    		while( $data=fread($fp,4096))
    		{
    			xml_parse( $xml_parser, $data, feof($fp)) or die( sprintf( "XML Error: %s at line %d",
    					   xml_error_string( xml_get_error_code( $xml_parser)),
    					   xml_get_current_line_number( $xml_parser)));
    		}
    
    		//Free the XML parser
    		xml_parser_free( $xml_parser);
    	}
    
    	private function fromxml_startnode( $parser, $element_name, $element_attributes)
    	{
    		//Handle the start of a node
    	}
    
    	private function fromxml_endnode( $parser, $element_name)
    	{
    		//Handle the end of a node
    	}
    
    	private function fromxml_data(  $parser, $data)
    	{
    		//Handle the content of a node
    	}
    
    	function EchoMe()
    	{
    		//Output the emails
    	}
    }
    
    $Emails = new Emails();
    $Emails->construct_fromxml_file( "xmltest.xml");
    ?>
    
    </body>
    </html>
    
    PHP:

    My xml file :


    <?xml version="1.0" encoding="ISO-8859-1"?>
    <notes>
    	<note>
    		<to>Danny</to><from>Gaz</from><title>Llandudno videos</title><body time="15:33:05" date="20080327">Hi Mate, struggling to get my computer to create a dvd. Should figure it out soon...</body>
    	</note>
    	<note>
    		<to>Gaz</to>
    		<from>Dave</from>
    		<title>Meal</title>
    		<body time="09:28:41" date="20080324">Am free next week if you are?</body>
    	</note>
    	<note>
    		<to>Vicki</to>
    		<from>Neil</from>
    		<title>Tomorrow night</title>
    		<body time="20:00:10" date="20080326">Hi Neil do you want some food tomorrow night when you come round?</body>
    	</note>
    </notes>
    
    
    HTML:
     
    GazPoint, Mar 30, 2008 IP
  2. GazPoint

    GazPoint Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Figured it out :

    http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback

     
    GazPoint, Apr 4, 2008 IP