
 rakesh v maheshwaram - 2006-11-14 14:56:15
hi friend
i am rakesh maheshwaram from Surat-Gujarat. Actually i'm facing problem when reading XML File.
Actually i am using php ver 4.4.4. i want load data from the xml file for that i am using domxml. this code work fine in php ver 5 but not worked in ver 4.4 
the code is like this
//books.xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<books>
  <book>
  <author>Jack Herrington</author>
  <title>PHP Hacks</title>
  <publisher>O'Reilly</publisher>
  </book>
  <book>
  <author>Jack Herrington</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
  </book>
  </books>
//php file
<?php
  $doc = new DOMDocument();
  $doc->load( 'books.xml' );
  
  $books = $doc->getElementsByTagName( "book" );
  foreach( $books as $book )
  {
  $authors = $book->getElementsByTagName( "author" );
  $author = $authors->item(0)->nodeValue;
  
  $publishers = $book->getElementsByTagName( "publisher" );
  $publisher = $publishers->item(0)->nodeValue;
  
  $titles = $book->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  
  echo "$title - $author - $publisher\n";
  }
  ?>
the server shows error like this:
Parse error: parse error, unexpected T_OBJECT_OPERATOR in C:\Apache 
Group\Apache2\htdocs\Formphp\test2.php on line 21
if i am removed all this  ->items(0) from the code like  $author = $authors->nodeValue; then it shows another error 
Fatal error: Cannot instantiate non-existent class: domdocument in C:\Apache Group\Apache2\htdocs\Formphp\test2.php on line 14
2) i am trying to read another xml file
//mymovies.xml
<?xml version="1.0" encoding="utf-8" ?>
   <movies>
      <movie>
         <title>Star Wars</title>
         <year>1977</year>
      </movie>
      <movie>
         <name>Apocalypse Now</name>
         <year>1979</year>
      </movie>
      <movie>
         <title>Raiders Of The Lost Ark</title>
         <year>1981</year>
      </movie>
   </movies>
<?php
	if (!$myxml=simplexml_load_file('mymovies.xml')){
		echo 'Error reading the XML file';
	}
	
	foreach($myxml as $movie){
		echo 'Title: ' . $movie->title . '<br />';	
		echo 'Year: ' . $movie->year . '<br /><hr />';	
	}	
?>
in this examle the server shows error like this
Fatal error: Call to undefined function: simplexml_load_file() in C:\Apache Group\Apache2\htdocs\Formphp\test2.php on line 4
Ragsrds
Rakesh maheshwaram