search a xml file with php

Discussion in 'PHP' started by Edynas, Apr 7, 2006.

  1. #1
    Hi,

    I have a very large file in xml (90mb) and altho I am not quite familiar with xml I now have a parser that can read the file....or at least a bit because I took a sample of 225kb and the parser works.

    Now I have a problem
    the xml file is about houses and I only want to parse houses in my area. So I am looking for a way to do a search within the parser. What I have now is this

    <?php
    // the xml file we want to parse
    $xmlSource="houses1.xml";
    
    $country="";
    $area="";
    $city="";
    $description_1_nl="";
    $description_2_nl="";
    $description_3_nl="";
    $house_id="";
    
    
    $currentElement="";	//holds the name of the current element
    
    $shows=array(); 	//array to hold all the house data
    
    /*	The start Element Handler	
    *	This is where we store the element name, currently being parsed, in $currentElement.
    *	This is also where we get the attribute, if any.
    */
    function startElement($parser,$name,$attr){
    		
    	$GLOBALS['currentElement']=$name;	
    	
    	/*	if the element is house, we want the value of the houseid attribute*/
    	if(strcmp($name,"house")==0){
    		$GLOBALS['house_id']=$attr["houseid"];
    	}
    }//end startElement()
    
    
    /*	
    *	The end Element Handler
    */
    
    function endElement($parser,$name){
    	$elements=array('country','area','city','description_1_nl','description_2_nl','description_3_nl','house_id');     
    
    
          /*If the element being parsed is a description it means that the
    
    *parser has completed parsing description. We can then store
    
    * the data in our array $shows[ ]
    
          */
    
          if(strcmp($name,"descriptions")==0){
    			foreach($elements as $element){
    				$temp[$element]=$GLOBALS[$element];							
    			}
    				$GLOBALS['shows'][]=$temp;
               
    
    
               /*After storing the data we reset our global description-variables to
               hold a new description*/
    
                $GLOBALS['description_1_nl']="";
    
                $GLOBALS['description_2_nl']="";
    
                $GLOBALS['description_3_nl']="";
    
          }
    
          /*After parsing a house we reset the rest of the globals.*/
    
          if(strcmp($name,"house")==0){
    
                $GLOBALS['house_id']="";
    
                $GLOBALS['area']="";
    
                $GLOBALS['country']="";
    
                $GLOBALS['city']="";
    
          }
    
    }//end endElement()
    
    
    /*	The character data Handler
    *	Depending on what the currentElement is, 
    *	the handler assigns the value to the appropriate variable
    */
    
    function characterData($parser, $data) {
            $elements = array (
                 'country','area','city','description_1_nl','description_2_nl','description_3_nl'
            );
    
            foreach ($elements as $element) {
                if ($GLOBALS["currentElement"] == $element) {
                    $GLOBALS[$element] .= $data;
                }
            }
        }
    
    /*	This is where the actual parsing is going on.
    *	parseFile() parses the xml document and return an array
    *	with the data we asked for.
    */
    
    function parseFile(){
    	global $xmlSource,$shows;
    	
    	/*Creating the xml parser*/
    	$xml_parser=xml_parser_create();
    	
    	/*Register the handlers*/
    	xml_set_element_handler($xml_parser,"startElement","endElement");
    	xml_set_character_data_handler($xml_parser,"characterData");
    	
    	/*Disables case-folding. Needed for this example*/
    	xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,false);
    	
    	/*Open the xml file and feed it to the parser in 4k blocks*/
       if(!($fp=fopen($xmlSource,"r"))){
          die("Cannot open  $xmlSource  ");
       }
       while(($data=fread($fp,4096))){
    
          if(!xml_parse($xml_parser,$data,feof($fp))){
    	     die(sprintf("XML error at line %d column %d ", 
                          xml_get_current_line_number($xml_parser), 
                          xml_get_current_column_number($xml_parser)));
    	  }
       }
         
    	 /*Finish ! we free the parser and returns the array*/
    	 xml_parser_free($xml_parser);
    	 
    	 return $shows;
    	
    }//end parseFile()
    PHP:
    If I search for xml php parse search in google I don't get any results I can work with so any help is welcome.

    Alternativly I am thinking of dumping the data from xml into a mysql database because I do know how to handle it if it's in mysql

    Edwin
     
    Edynas, Apr 7, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Thats probably what I would do as well.:)
     
    mad4, Apr 7, 2006 IP
  3. North Carolina SEO

    North Carolina SEO Well-Known Member

    Messages:
    1,327
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    105
    #3
    You might check out the XSL side of the equation. Perform a search with "XSL filter" (without the quotes) to see how others have used the stylesheet to apply filters to the data. Good luck!
     
    North Carolina SEO, Jul 18, 2006 IP