Hi,

I am having a couple of difficulties with a script to parse XML. This may
well be because my 16 month kid was up all night, but I'm banging my head
against a wall right now.

The script quite easily extracts one of the entries listed under <DATA_2>
but doesnt list the other. This is my first look into parsing XML, so I am
not sure of the terminology :-). How can I have both entries in this XML
file parsed to an array that I can use to build a table. (I just really need
the array, I can take it from there).

Thanks !

Phil

Here's my code (please let me know if I'm missing anything really simple):

============================
== PHP CODE
============================
<?
 $data = file_get_contents('db_list.xml');
 $ret = array();
 $hash_stack = array();

 $parser = xml_parser_create();
 xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
 xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
 xml_parse_into_struct($parser,$data,$values,$tags);
 xml_parser_free($parser);


 foreach ($values as $key => $val) {

   if($val['type'] == "open") {
         array_push($hash_stack, $val['tag']);
     }
   elseif($val['type'] == "close") {
         array_pop($hash_stack);
     }
   elseif($val['type'] == "complete") {
         array_push($hash_stack, $val['tag']);
         eval("\$ret[" . implode($hash_stack, "][") . "] =
'{$val[value]}';");
         array_pop($hash_stack);
     }

  }
print_r($ret);
?>

============================
== XML CODE
============================

<?xml version="1.0"?>
<DATA_DB>
  <LIST_2>
    <DATA_2>
      <DB_KEY>09011234</DB_KEY>
      <LIST_1>
        <G_1>
          <NAME>BLOGGS, JOE</NAME>
           <LIST_15>
            <DATA_15>
              <ADDRESS>15 MAIN STREET</ADDRESS>
              <CITY_STATE>MAINLAND  WS</CITY_STATE>
              <ZIP>99999</ZIP>
            </DATA_15>
          </LIST_15>
        </G_1>
      </LIST_1>
    </DATA_2>
    <DATA_2>
      <DB_KEY>09011235</DB_KEY>
      <LIST_1>
        <G_1>
          <NAME>BROOK, CURTIS</NAME>
          <LIST_15>
            <DATA_15>
              <ADDRESS>16 MAIN STREET</ADDRESS>
              <CITY_STATE>MAINLAND  WS</CITY_STATE>
              <ZIP>99999</ZIP>
            </DATA_15>
          </LIST_15>
        </G_1>
      </LIST_1>
    </DATA_2>
  </LIST_2>
</DATA_DB>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to