php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13811 Fatal error: Call to undefined function: xmldocfile()
Submitted: 2001-10-24 05:52 UTC Modified: 2001-10-24 06:01 UTC
From: control1 at dreamwiz dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 4.0.6 OS: NT2000
Private report: No CVE-ID: None
 [2001-10-24 05:52 UTC] control1 at dreamwiz dot com
// data file
$file = "library.xml";
// create a document object
$dom = xmldocfile($file);


Display Error Message

Fatal error: Call to undefined function: xmldocfile() in d:\apache group\apache\htdocs\test\phpxml2.html on line 28

//-- source
// data file - library.xml
// success html - phpxml.html
// fail html    - phpxml2.html


//----------------------------------------------------------
// data file - library.xml
//----------------------------------------------------------

<?xml version="1.0"?>

<library>
	<book>
		<title>Hannibal</title>
		<author>Thomas Harris</author>
		<genre>Suspense</genre>
		<pages>564</pages>
		<price>8.99</price>
		<rating>4</rating>
	</book>

	<book>
		<title>Run</title>
		<author>Douglas E. Winter</author>
		<genre>Thriller</genre>
		<pages>390</pages>
		<price>7.49</price>
		<rating>5</rating>
	</book>

	<book>
		<title>The Lord Of The Rings</title>
		<author>J. R. R. Tolkien</author>
		<genre>Fantasy</genre>
		<pages>3489</pages>
		<price>10.99</price>
		<rating>5</rating>
	</book>

</library>

//----------------------------------------------------------
// success html - phpxml.html
//----------------------------------------------------------
<html>                                                                   
<head>                                                                   
<title>The Library</title>                                               
<style type="text/css">                                                  
TD {font-family: Arial; font-size: smaller}                              
H2 {font-family: Arial}                                                  
</style>                                                                 
</head>                                                                  
<body bgcolor="white">                                                   
<h2>The Library</h2>                                                     
<table border="1" cellspacing="1" cellpadding="5">                       
<tr>                                                                     
	<td align=center>Title</td>                                            
	<td align=center>Author</td>                                           
	<td align=center>Price</td>                                            
	<td align=center>User Rating</td>                                      
</tr>                                                                    
                                                                         
<?                                                                       
// data file                                                             
$file = "library.xml";                                                   
                                                                         
// use this to keep track of which tag the parser is currently processing
$currentTag = "";                                                        
                                                                         
function startElement($parser, $name, $attrs) {                          
	global $currentTag;                                                    
	$currentTag = $name;                                                   
                                                                         
	// output opening HTML tags                                            
	switch ($name) {                                                       
	case "BOOK":                                                           
		echo "<tr>";                                                         
        break;                                                           
                                                                         
	case "TITLE":                                                          
		echo "<td>";                                                         
		break;                                                               
                                                                         
	case "AUTHOR":                                                         
		echo "<td>";                                                         
		break;                                                               
                                                                         
	case "PRICE":                                                          
		echo "<td>";                                                         
		break;                                                               
                                                                         
	case "RATING":                                                         
		echo "<td>";                                                         
		break;                                                               
                                                                         
	default:                                                               
		break;                                                               
	}                                                                      
}                                                                        
                                                                         
function endElement($parser, $name) {                                    
	global $currentTag;                                                    
                                                                         
	// output closing HTML tags                                            
	switch ($name) {                                                       
	case "BOOK":                                                           
		echo "</tr>";                                                        
		break;                                                               
                                                                         
	case "TITLE":                                                          
		echo "</td>";                                                        
		break;                                                               
                                                                         
	case "AUTHOR":                                                         
		echo "</td>";                                                        
		break;                                                               
                                                                         
	case "PRICE":                                                          
		echo "</td>";                                                        
		break;                                                               
                                                                         
	case "RATING":                                                         
		echo "</td>";                                                        
		break;                                                               
                                                                         
	default:                                                               
		break;                                                               
	}                                                                      
                                                                         
	// clear current tag variable                                          
	$currentTag = "";                                                      
}                                                                        
                                                                         
// process data between tags                                             
function characterData($parser, $data) {                                 
                                                                         
	global $currentTag;                                                    
	// text ratings                                                        
	$ratings = array("Words fail me!", "Terrible", "Bad", "Indifferent",   
"Good", "Excellent");                                                    
                                                                         
	// format the data                                                     
	switch ($currentTag) {                                                 
	case "TITLE":                                                          
		// italics for title                                                 
		echo "<i>$data</i>";                                                 
		break;                                                               
                                                                         
	case "AUTHOR":                                                         
		echo $data;                                                          
		break;                                                               
                                                                         
	case "PRICE":                                                          
		// add currency symbol for price                                     
		echo "$" . $data;                                                    
		break;                                                               
                                                                         
	case "RATING":                                                         
		// get text rating                                                   
		echo $ratings[$data];                                                
		break;                                                               
                                                                         
	default:                                                               
		break;                                                               
	}                                                                      
}                                                                        
                                                                         
// initialize parser                                                     
$xml_parser = xml_parser_create();                                       
                                                                         
// set callback functions                                                
xml_set_element_handler($xml_parser, "startElement", "endElement");      
xml_set_character_data_handler($xml_parser, "characterData");            
                                                                         
// open XML file                                                         
if (!($fp = fopen($file, "r")))                                          
{                                                                        
    die("Cannot locate XML data file: $file");                           
}                                                                        
                                                                         
// read and parse data                                                   
while ($data = fread($fp, 4096))                                         
{                                                                        
    // error handler                                                     
	if (!xml_parse($xml_parser, $data, feof($fp)))                         
	{                                                                      
    die(sprintf("XML error: %s at line %d",                              
xml_error_string(xml_get_error_code($xml_parser)),                       
xml_get_current_line_number($xml_parser)));                              
    }                                                                    
}                                                                        
                                                                         
// clean up                                                              
xml_parser_free($xml_parser);                                            
                                                                         
?>                                                                       
                                                                         
</table>                                                                 
</body>                                                                  
</html>                                                                  


//----------------------------------------------------------
// fail html    - phpxml2.html
//----------------------------------------------------------

<html>
<head>
<title>The Library</title>
<style type="text/css">
TD {font-family: Arial; font-size: smaller}
H2 {font-family: Arial}
</style>
</head>
<body bgcolor="white">
<h2>The Library</h2>
<table border="1" cellspacing="1" cellpadding="5">
<tr>
	<td align=center>Title</td>
	<td align=center>Author</td>
	<td align=center>Price</td>
	<td align=center>User Rating</td>
</tr>

<?

// text ratings
$ratings = array("Words fail me!", "Terrible", "Bad", "Indifferent",
"Good", "Excellent");

// data file
$file = "library.xml";
// create a document object
$dom = xmldocfile($file);

// get reference to root node
$root = $dom->root();
// array of root node's children - the <book> level
$nodes = $root->children();

// iterate through <book>s
for ($x=0; $x<sizeof($nodes); $x++)
{
	// new row
	echo "<tr>";

		// check type
		// this is to correct whitespace (empty nodes)
		if ($nodes[$x]->type == XML_ELEMENT_NODE)
		{
		$thisNode = $nodes[$x];
		// get an array of this node's children - the <title>, <author> level
		$childNodes = $thisNode->children();

			// iterate through children
			for($y=0; $y<sizeof($childNodes); $y++)
			{
				// check type again
				if ($childNodes[$y]->type == XML_ELEMENT_NODE)
				{
					// appropriate markup for each type of tag
					// like a switch statement
					if ($childNodes[$y]->name == "title")
					{
					echo "<td><i>" . $childNodes[$y]->content . "</i></td>";
					}

					if ($childNodes[$y]->name == "author")
					{
					echo "<td>" . $childNodes[$y]->content . "</td>";
					}

					if ($childNodes[$y]->name == "price")
					{
					echo "<td>$" . $childNodes[$y]->content . "</td>";
					}

					if ($childNodes[$y]->name == "rating")
					{
					echo "<td>" . $ratings[$childNodes[$y]->content] . "</td>";
					}

				}
			}

		}
	// close the row tags
	echo "</tr>";
}


?>
</table>
</body>
</html>

 






Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-24 06:01 UTC] sniper@php.net
Please ask support questions on the mailing lists:

http://www.php.net/support.php

hint: Enable php_domxml.dll in php.ini..


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC