Jump to content

XML parsing help... getting the name of the parent node


Syco54645

Recommended Posts

I am beating my head off the pavement here trying to get the name of the parent node.  I tried using an array_push, but being that only the startElement can accept a third parameter (or so it seems) that will not work.

 

This is an example of my xml

 

<blah>
   <all_station_numbers>
      <id2>
         <stationId>411</stationId>
         <subStationId>2</subStationId>
         <commonName>9652</commonName>
         <service>EMS</service>
         <memberId>29</memberId>
         <updated>2007-08-17 09:50:25</updated>
         <groupid>9</groupid>
      </id2>
      <id4>
         <stationId>142</stationId>
         <subStationId>2</subStationId>
         <commonName>7541</commonName>
         <service>POLICE</service>
         <memberId>29</memberId>
         <updated>2007-08-17 09:50:41</updated>
         <groupid>9</groupid>
      </id4>
   </all_station_numbers>
</blah>

 

 

And the php that I am working with trying to parse this.

 

<?php
    function data($parser, $data){
        //echo $data;
        if(strtolower($data)==strtolower($_POST['quick']))
        {
            echo "we got a match<br>";
        }
    }
    
    function startElement($parser, $data, $ary){
        array_push($ary, $data);
    }
    
    function endElement($parser, $data){
        
    }
    
    $starttimer = time()+microtime();

    $file = "db.xml";
    
    $ary=array();
    
    $xml_parser = xml_parser_create();
    
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    
    xml_set_character_data_handler($xml_parser, "data");
    
    $fp = fopen($file, "r");
    
    $data = fread($fp, 80000);
    
    if(!(xml_parse($xml_parser, $data, feof($fp)))){
        die("Error on line " . xml_get_current_line_number($xml_parser));
    }
    
    xml_parser_free($xml_parser);
    
    fclose($fp);
    
    
    $stoptimer = time()+microtime();

    $timer = round($stoptimer-$starttimer,4);

    echo "Search completed in $timer seconds.";
?> 

 

This xml is a dump of my database that I would like to run a quicksearch on.  All_station_numbers is the table and id2, id3, etc is the unique id of each row.  I need this information to know what kind of link to present the user with when a result is found.  I have used the parsing in .net and it had something like parent(element), does php have anything like this?  All of my searches have returned nothing.

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.