XML Parsing in PHP - What's going on?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcrevys
Forum Newbie
Posts: 1
Joined: Wed Jan 04, 2006 4:44 pm
Location: Chicago

XML Parsing in PHP - What's going on?!

Post by mcrevys »

Twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Hey guys,

I've got an XML file I need to chop up using PHP, but for some reason for those tags that have large chunks of text, text gets cropped and I only get the last 1K of data for these text fields. What's going on? I've attached the code - any help will be appreciated!

Code: Select all

class xmlParser{

   var $xml_obj = null;
   var $output = array();
  
   function xmlParser(){
     
       $this->xml_obj = xml_parser_create();
       xml_set_object($this->xml_obj,$this);
       xml_set_character_data_handler($this->xml_obj, 'dataHandler');  
       xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
  
   } 
  
   function parse($path){
     
       if (!($fp = fopen($path, "r"))) {
           die("Cannot open XML data file: $path");
           return false;
       }
     
//       while ( $data = fread( $fp, 4096 ) ) {
//		   if (!xml_parse_into_struct($this->xml_obj, $data, feof($fp))) {
		   $data = fread($fp, filesize($path));
		   if (!xml_parse_into_struct($this->xml_obj, $data, $vals, $index)) {
               die(sprintf("XML error: %s at line %d",
               xml_error_string(xml_get_error_code($this->xml_obj)),
               xml_get_current_line_number($this->xml_obj)));
               xml_parser_free($this->xml_obj);
           }
       
     
       return true;
   }
 
   
   function startHandler($parser, $name, $attribs){
       $_content = array('name' => $name);
       if(!empty($attribs))
         $_content['attrs'] = $attribs;
       array_push($this->output, $_content);
   }

   function dataHandler($parser, $data){
       if (!empty($data)) {
           $_output_idx = count($this->output) - 1;
           $this->output[$_output_idx]['content'] = $data;
       }
   }

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

	$p =& new xmlParser();
	$p->parse( 'properties_reply_short.xml' );
	$c = $p->output;
Post Reply