Page 1 of 1

Parsing multiple xml files to PHP

Posted: Wed Oct 17, 2007 12:53 pm
by prcrstntr2
Hello all you savvy developers! Ive got one that i cant answer that you might be able to! Please Help!

I am currently parsing single xml file to php4 using this method:

Code: Select all

<?php
 $p =& new xmlParser();
 $p->parse("myxmlfile.xml", "nextfile.xml");  // i need to parse more than one at a time.
 echo "<fasWork>";
 //print_r($p->output);
 echo "</fasWork>";
	
class xmlParser{
   var $xml_obj = null;
   var $output = array();
   var $attrs;

   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($this->xml_obj, $data, feof($fp))) {
               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();
        $_content['name'] = $name;
        if(!empty($attribs))
            $_content['attrs'] = $attribs;
        array_push($this->output, $_content);
}

   function dataHandler($parser, $data){
        if(!empty($data) && $data!="\n") {
            $_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;
            $add = array();
            if(!$this->output[$_output_idx]['child'])
                $this->output[$_output_idx]['child'] = array();
            array_push($this->output[$_output_idx]['child'], $_data);
        }   
   }
}


function removeArrayElement(&$arr, $index){
    if(isset($arr[$index])){
        array_splice($arr, $index, 1);
    }
}

$parsed_data = ($p->output);

?>
My problem is that I need to parse multiple files at a time as commented in the above code. Right now it only displays the structure of the first file. i also tried a foreach loop with an array of filenames but that only returned a fatal error.

Any ideas?

Posted: Wed Oct 17, 2007 2:00 pm
by Christopher
If you want a loop, I think you need to move the parser creation code into the parse() method, and add free/close code.

Posted: Wed Oct 17, 2007 3:11 pm
by prcrstntr2
I didnt want to free anything in the function because i need to set them into seperate arrays and hold on to them for later use. But someone helped me figure it out.

instead of looping the entire function, i looped just the array builder in the beginning and gave each array a unique id.

Code: Select all

$files = array("first.xml", "next.xml"); 
 $all_arrys = count($files);
 $p = array(); 

foreach ($files as $i=>$file) {
 $p[$i] =& new xmlParser();
 $p[$i]->parse($file);
 $parsed_data[$i] = ($p[$i]->output);
 print_r($parsed_data[$i]);
}
thanks for helping though!