Jump to content

[SOLVED] XML Parser Help


R3p1v

Recommended Posts

I just upgraded PHP from version 4 to 5 and this parser stopped working. Can someone help me fix it please? Thanks!

 

(I replaced sensitive data with "REMOVED")

 

 

<?php

class XMLParser {
   var $filename;
   var $xml;
   var $data;
   
   function XMLParser($xml_file)
   {
       $this->filename = $xml_file;
       $this->xml = xml_parser_create();
       xml_set_object($this->xml, $this);
       xml_set_element_handler($this->xml, 'startHandler', 'endHandler');
       xml_set_character_data_handler($this->xml, 'dataHandler');
       $this->parse($xml_file);
   }
   
   function parse($xml_file)
   {
  	$feed = "$xml_file";
    	$ch = curl_init($feed);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    	curl_setopt($ch, CURLOPT_REFERER, "http://REMOVED"); 
$fp = trim(curl_exec($ch));
   	curl_close($ch);

if (!$fp) {
             die('Cannot open XML data file: '.$xml_file);
               return false;
       }

       $bytes_to_parse = 512;

       
           $parse = xml_parse($this->xml, $fp);
           
           if (!$parse) {
               die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($this->xml)),
                       xml_get_current_line_number($this->xml)));
                       xml_parser_free($this->xml
                     );
           }
       

       return true;
   }
   
   function startHandler($parser, $name, $attributes)
   {
       $data['name'] = $name;
       if ($attributes) { $data['attributes'] = $attributes; }
       $this->data[] = $data;
   }

   function dataHandler($parser, $data)
   {
       if ($data = trim($data)) {
           $index = count($this->data) - 1;
           // begin multi-line bug fix (use the .= operator)
           $this->data[$index]['content'] .= $data;
           // end multi-line bug fix
       }
   }

   function endHandler($parser, $name)
   {
       if (count($this->data) > 1) {
           $data = array_pop($this->data);
           $index = count($this->data) - 1;
           $this->data[$index]['child'][] = $data;
       }
   }
}


$url = "REMOVED";
$myFile = new XMLParser($url);
$data = $myFile->data;

/////Variables assigned here, removed//////

?>

 

Link to comment
https://forums.phpfreaks.com/topic/76718-solved-xml-parser-help/
Share on other sites

Sorry to keep bumping this =/ I need to get this solved quick though.

 

Since its displaying that error, I guess the problem is in these few lines. Why is this on working on PHP 5?

 

   function parse($xml_file)
   {
  	$feed = "$xml_file";
    	$ch = curl_init($feed);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    	curl_setopt($ch, CURLOPT_REFERER, "http://REMOVED"); 
$fp = trim(curl_exec($ch));
   	curl_close($ch);

if (!$fp) {
             die('Cannot open XML data file: '.$xml_file);
               return false;
       }

Try changing those lines to this

 

<?php

   function parse($xml_file)
   {
  	$feed = trim($xml_file);
    	$ch = curl_init($feed);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    	curl_setopt($ch, CURLOPT_REFERER, "http://REMOVED"); 
$fp = curl_exec($ch);
   	curl_close($ch);

if (!$fp) {
             die('Cannot open XML data file: '.$xml_file);
               return false;
       }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.