p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: XML Parsing Error


Message #1 by David@s... on Thu, 30 May 2002 11:09:26 -0400
I've been trying to figure this out for 2 hours now and can't find a
solution. Hope you guys  can help me out. I am not that great with PHP
yet...
I am attempting to build a class that uses "xml_parser_create()". I am able
to open the xml file alright but then I get an error: "XML error: no element
found at line 1"

I can't seem to find any way to debug this thing. I guess my only hope is to
post the code and hope that one of you kind souls will take pity on me...
Sorry for the super-long post!

Thanks in advance, 
Dave



Here is the XML file:

<?xml version="1.0"?>
<config>
	<root_dir>c:\apache\htdocs</root_dir>	
	<server_root>htdocs</server_root>	
	<portal_title>Title goes here...</portal_title>	
	<skip_lobby_page>False</skip_lobby_page>	
	<show_course>All</show_course>
</config>

Here is the code:

<?

define(XML_FILE, "config.xml");

class ProcessConfig
{
	var $xml_parser;
	// use this to keep track of which tag the parser is currently
processing
	var $currentTag = "";
	
	var $xmldoc;
	var $m_root;
	var $m_serverRoot;
	var $m_title;
	var $m_skiplobby;
	var $m_showcourses;
	
	
//--------------------------------------------------------------------------
-------------------------------------------------------------
	
	function class_initialize()
	{
		
	}
	
	
//--------------------------------------------------------------------------
-------------------------------------------------------------
	
	
	function parse()
	{
		// create the parser reference
		$this->$xml_parser = xml_parser_create();
		// open up the XML file
		if (!($fp = fopen(XML_FILE, "r")))
		{
			die("Cannot locate XML data file: $file");
		}
		
		
		// set callback functions
		xml_set_element_handler($this->$xml_parser,
"$this->startElement", "$this->endElement");
		xml_set_character_data_handler($this->$xml_parser,
"$this->characterData");
		
		while ($data = fread($fp, 4096))
		{
			echo $data;
			
			// error handler		
			if (!xml_parse($this->$xml_parser, $this->$data,
feof($fp)))
			{
				die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->$xml_parser)),
xml_get_current_line_number($this->$xml_parser)));
			}
			
		}	
	}
	
	
//--------------------------------------------------------------------------
-------------------------------------------------------------
	
	function startElement($parser, $name, $attrs)
	{
		
		static $counter;
		global $currentTag;
		global $currentID;
		
		$currentTag = $name;
		//$currentID = "12";
		
		echo "TAG: " . $currentTag;
		echo $name;
		
		switch ($name)
		{
			case "CONFIG":
				break;
			case "ROOT_DIR":
				break;			
		}
	
	}
	
	
//--------------------------------------------------------------------------
-------------------------------------------------------------
	
	function endElement($parser, $name)
	{
		global $currentTag;
		global $currentID;
		
	
		switch ($name)
		{
			case "CONFIG":
				break;
			case "ROOT_DIR":
				echo "</td></tr>";
				break;
		}

		$currentTag = "";
	}
	
	
//--------------------------------------------------------------------------
-------------------------------------------------------------
	
	function characterData($parser, $data)
	{
		//echo "ID: $currentID";
		
		global $currentTag;
		global $currentID;
		
		
		switch ($currentTag)
		{	
			
			case "CONFIG":
				break;
			case "ROOT_DIR":
				echo "<tr><td>$data";
				break;
			
		}
	}
	
	
//--------------------------------------------------------------------------
-------------------------------------------------------------
	
	function class_terminate()
	{
		xml_parser_free($xml_parser);
	}
	
	
//--------------------------------------------------------------------------
-------------------------------------------------------------
}

?>
Message #2 by "Nikolai Devereaux" <yomama@u...> on Thu, 30 May 2002 09:44:27 -0700
I've got the feeling that one of your problems lies here:

> // set callback functions
> xml_set_element_handler($this->$xml_parser,
>                         "$this->startElement",
>                         "$this->endElement");
> xml_set_character_data_handler($this->$xml_parser,
>                                "$this->characterData");

The reason is because you're using double quoted strings, and therefore the
$this-> stuff is parsed and dereferenced.

Take a look at the example for xml_set_object:
  http://www.php.net/xml_set_object

That should allow you to pass a plain string to the set_xxx_handler
functions and have them be interpreted as member functions of an object.


Take care,

Nik


  Return to Index