Author Topic: Fatal error: Call to undefined function SimpleXMLElement() Help  (Read 2321 times)

0 Members and 1 Guest are viewing this topic.

Offline fubowlTopic starter

  • Irregular
  • Posts: 37
    • View Profile
Using the script located here: http://www.phpfreaks.com/tutorial/handling-xml-data

I receive the following error: Fatal error: Call to undefined function SimpleXMLElement() in C:\...\xml.php on line 7

xml.php:
<?php
// Passing the XML
$data file_get_contents("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF'])."/album_output.php"); //loads fine

$books SimpleXMLElement($data);

foreach(
$books as $book// loop through our books
{
	
echo <<<EOF
	
<tr>
	
	
<td>
{$book->url}</td>
	
	
<td>
{$book->description}</td>
	
	
<td>
{$book->filename}</td>
	
</tr>

EOF;
}
?>


Running WAMPServer with php 5.3.0

phpinfo(); says:

Simplexml support   enabled
Revision    $Revision: 1.151.2.22.2.35.2.32 $
Schema support    enabled

However, unable to find an extension for simplexml inside php.ini or httpd.conf (if it's even supposed to be in there)

Any help would be appreciated.

Offline fubowlTopic starter

  • Irregular
  • Posts: 37
    • View Profile
Re: Fatal error: Call to undefined function SimpleXMLElement() Help
« Reply #1 on: August 26, 2010, 09:26:41 PM »
Well, since I received no answers, I found an alternative solution.

Using DOM method to access XML file:


  
// DOMElement->getElementsByTagName() -- Gets elements by tagname
  // nodeValue : The value of this node, depending on its type.
  // Load XML File. You can use loadXML if you wish to load XML data from a string

  
$objDOM = new DOMDocument();
  
$objDOM->load("test.xml"); //make sure path is correct


  
$note $objDOM->getElementsByTagName("thumbnail");
  
// for each note tag, parse the document and get values for
  // tasks and details tag.

  
foreach( $note as $value )
  {
    
$tasks $value->getElementsByTagName("filename");
    
$task  $tasks->item(0)->nodeValue;

    
$details $value->getElementsByTagName("url");
    
$detail  $details->item(0)->nodeValue;

    echo 
"$task :: $detail <br>";
  }


where test.xml:

Code: [Select]
<thumbnails>
<thumbnail>
<filename>http://thumb1</filename>
<url>Set 1 URL</url>
<description>Album 1</description>
</thumbnail>
<thumbnail>
<filename>http://thumb2</filename>
<url>Set 2 URL</url>
<description>Album 2</description>
</thumbnail>
<thumbnail>
<filename>http://thumb3</filename>
<url>Set 3 URL</url>
<description>Album 3</description>
</thumbnail>
<thumbnail>
<filename>http://thumb4</filename>
<url>Set 4 URL1</url>
<description>Album 4</description>
</thumbnail>
</thumbnails>

or to get xml attribute:

// example on using the 'getAttribute()' method

$dom=new DOMDocument();

$dom->load('test2.xml');

$headlines=$dom->getElementsByTagName('thumbnail');

foreach(
$headlines as $headline){

echo 
'ID attribute of current node is the following: '.$headline->getAttribute('filename').'<br />';

}


where test2.xml:

Code: [Select]
<thumbnails>
<thumbnail filename="http://thumb1" url="72157620019981688" description="Cat 1"/>
<thumbnail filename="http://thumb2" url="72157619839911144" description="Cat 2"/>
<thumbnail filename="http://thumb3" url="72157619840761942" description="Cat 3"/>
<thumbnail filename="http://thumb4" url="72157619935624301" description="Cat 4"/>
</thumbnails>