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.

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.