Hello Kids.

I am learning XML and so to do this i am importing my mate DVDProfiler DVD collection into mysql then back out again etc (yes its stilly but it lets me learn how to create and read xml files)

Anyway below is the code dump and the file i am using.

Anyway my question is that I am reading the whole contents of the XML file and parsing into an array (= big mem overheads). Is there anyway to only parse the first record of the 250 or so that are in the xml file without using an array.

i.e. only info in first <DVD> </DVD> is loaded into array? Or only first 5 dvd are loaded into the array?

Thanks for your time. Richard.

    <?
    // To call the function
    $tree = GetXMLTree('collection.xml');
    
    mysql_connect("localhost", "richard", "x");
    mysql_select_db("CONTENT");
    
    $added_num = 0;
    
    function GetChildren($vals, &$i)
    {
      $children = array();     // Contains node data
    
      /* Node has CDATA before it's children */
      if (isset($vals[$i]['value']))
        $children['VALUE'] = $vals[$i]['value'];
    
      /* Loop through children */
      while (++$i < count($vals))
      {
        switch ($vals[$i]['type'])
        {
          /* Node has CDATA after one of it's children
            (Add to cdata found before if this is the case) */
          case 'cdata':
            if (isset($children['VALUE']))
              $children['VALUE'] .= $vals[$i]['value'];
            else
              $children['VALUE'] = $vals[$i]['value'];
            break;
          /* At end of current branch */
          case 'complete':
            if (isset($vals[$i]['attributes'])) {
              $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
              $index = count($children[$vals[$i]['tag']])-1;
    
          if (isset($vals[$i]['value']))
            $children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
          else
            $children[$vals[$i]['tag']][$index]['VALUE'] = '';
        } else {
          if (isset($vals[$i]['value']))
            $children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
          else
            $children[$vals[$i]['tag']][]['VALUE'] = '';
    	}
        break;
      /* Node has more children */
      case 'open':
        if (isset($vals[$i]['attributes'])) {
          $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
          $index = count($children[$vals[$i]['tag']])-1;
          $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],GetChildren($vals, $i));
        } else {
          $children[$vals[$i]['tag']][] = GetChildren($vals, $i);
        }
        break;
      /* End of node, return collected data */
      case 'close':
        return $children;
    }
      }
    }
    
    /* Function will attempt to open the xmlloc as a local file, on fail it will attempt to open it as a web link */
    function GetXMLTree($xmlloc)
    {
      if (file_exists($xmlloc))
        $data = implode('', file($xmlloc));
      else {
        $fp = fopen($xmlloc,'r');
        $data = fread($fp, 100000000);
        fclose($fp);
      }
    
      $parser = xml_parser_create('ISO-8859-1');
      xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
      xml_parse_into_struct($parser, $data, $vals, $index);
      xml_parser_free($parser);
    
      $tree = array();
      $i = 0;
    
      if (isset($vals[$i]['attributes'])) {
    	$tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
    	$index = count($tree[$vals[$i]['tag']])-1;
    	$tree[$vals[$i]['tag']][$index] =  array_merge($tree[$vals[$i]['tag']][$index], GetChildren($vals, $i));
      }
      else
        $tree[$vals[$i]['tag']][] = GetChildren($vals, $i);
    
      return $tree;
    }
    
    foreach ($tree["COLLECTION"][0]["DVD"] as $num => $dvd_info) {
    
    // read stuff from array etc....  
    } // do stuff with teh variables... }; ?>
    
    Some fields removed to stop the mother of all code dumps.
    
    <?xml version="1.0" encoding="us-ascii"?>
    <!-- DVD Profiler Version 2.2.0 Collection Export -->
    <!-- Exported: 09/05/2004 19:02:14 -->
    <Collection>
    <DVD>
      <ID>0000000100</ID>
      <UPC>00000-00100</UPC>
      <Title>Pirates of the Caribbian</Title>
      <SortTitle>Pirates of the Caribbian</SortTitle>
      <Regions>
        <Region>1</Region>
      </Regions>
      <CollectionType>Owned</CollectionType>
      <Rating>PG-13</Rating>
      <ProductionYear>2003</ProductionYear>
      <Released>2003-12-02</Released>
      <RunningTime>143</RunningTime>
      <Genres>
        <Genre>Adventure</Genre>
      </Genres>
    </DVD>
    </Collection>
    

      You just need to split the data on each </dvd><dvd> you encounter. explode() will do it, but preg_split will do it with more flexibility (and fuzziness). Each bit you get back will contain a <dvd></dvd> bit, which you can parse.

      dave

        Doesn't preg_split just use another array though?

        Which part are you exploding?

          Write a Reply...