www.webdeveloper.com
Results 1 to 2 of 2

Thread: Exporting XML

  1. #1
    Join Date
    Jun 2005
    Posts
    332

    Exporting XML

    I've got this xml file, it is huge 442 mb dump. I want to export it into a mysql database. Now what I'm wondering is does php open the whole file when reading it, or does it do it only by what it finds. Because I want to export it but I'm afraid my computer wouldn't be able to open such a size, time does not matter I can leave my computer on. So is this the right way to do it, or is there a better way. Also not all of the XML I want to export, only parts of it and I want to be able to edit what goes into the mysql database. Thanks.
    There's no time like the present.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    I suspect that any of the built-in XML functions are going to end up loading everything into memory.

    You might be able to do something like use fopen() and fgets(), reading one line at a time. You then need logic to parse the line, set variables to be used in the SQL, and when you find an end-of-record element execute the current query; roughly something like:
    PHP Code:
    $fh fopen('xmlfile''r') or die('fopen error');
    $queryData = array();
    while((
    $line fgets($fh)) !== FALSE)
    {
       if(
    preg_match('/</end_of_record>/'$line))
       {
          
    // build query from data saved in $queryData, then execute it
          
    $queryData = array();  reinitialize
       
    }
       elseif(
    preg_match('/<tag1>([^<]*)</tag1>/'$line$matches))
       {
          
    $queryData['tag1'] = $matches[1];
       }
       elseif(
    preg_match('/<tag2>([^<]*)</tag2>/'$line$matches))
       {
          
    $queryData['tag2'] = $matches[1];
       }
       
    // etc. for each data element

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles