Parsing Taxonomy structure into HTML nested unordered list

I’m trying to load Google Base’s Taxonomy into a jQuery mcDropdown plugin.
I need a script to take a file formatted like this:


Arts & Entertainment
Arts & Entertainment > Collectibles
Arts & Entertainment > Collectibles > Autographs
Arts & Entertainment > Collectibles > Coins & Currency
Arts & Entertainment > Collectibles > Collectible Weapons
Arts & Entertainment > Collectibles > Collectible Weapons > Collectible Swords
Arts & Entertainment > Collectibles > Collectible Weapons > Sword Stands & Displays

and create an unordered list like this:


<ul id="categorymenu" class="mcdropdown_menu">
  <li rel="1">
    Arts & Entertainment
    <ul>
      <li rel="2">
        Collectibles
        <ul>
          <li rel="3">
            Autographs
          </li>
          <li rel="4">
            Coins & Currency
          </li>
          <li rel="5">
            Collectible Weapons
            <ul>
              <li rel="6">
                Collectible Swords              
              </li>
              <li rel="7">
                Sword Stands & Displays              
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

The following code shows nothing and no errors are
shown. What am I doing wrong?


<?php
  error_reporting(E_ALL);
  $doc = new DomDocument('1.0');
  $root = $doc->createElement('ul');
  $attr1 = $doc->createAttribute('id');
  $attr1->value = 'categorymenu';
  $root->appendChild($attr1);
  $attr1 = $doc->createAttribute('class');
  $attr1->value = 'mcdropdown_menu';
  $root->appendChild($attr1);
  $ul = $doc->appendChild($root);

  $file = fopen('e:\\google_base_taxonomy2.txt', "r");  
  $relctr = 1;
  while (!feof($file)) {
    $line = fgets($file);    
    $data = explode(" > ", $line);
    if(trim($data[0]) != $cat) {
      $cat = trim($data[0]);
      $ul = $doc->createElement('ul');
      $ul = $root->appendChild($ul);
    }
    foreach($data as value) {
      $li = $doc->createElement('li');
      $attr1 = $doc->createAttribute('rel');
      $attr1->value = $relctr;
      $li->appendChild($attr1);
      $li->value = $value;
      $li = $ul->appendChild($li);
    }            
  }
  $h = $doc->saveHTML();
  echo $h;
  fclose($file);
  unset($doc); 
?>