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
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;
       }

Link to comment
Share on other sites

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;
       }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.