how to split an xml file ?

Discussion in 'PHP' started by z80039, Oct 23, 2007.

  1. #1
    Hi ,
    I have an xml database which is 7 mb big and i need to split it in order to upload it to my server. How can I do that .
    Please note I have a very little to no idea about xml,php and databases.

    Regards
     
    z80039, Oct 23, 2007 IP
  2. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #2
    not sure this is in the right section...

    maybe you can rar it and split it?
     
    Lordy, Oct 23, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    I assume you know how to use php from a command line and that you have it installed on your own machine ...

    
    <?php
    /**
     * @author Interviolet
     * @package [$package]
     * @filename [$filename]
     * @copyright 2007
     */
    
    if( !defined( "YOUR_XML" ) )		define("YOUR_XML",		"Untitled-1.psd" );
    
    if( ( $read = fopen( YOUR_XML, 'r' ) ) )
    {
    	if( ( $write = gzopen( sprintf( '%s.gz', YOUR_XML ), 'w' ) ) )
    	{
    		while( !feof( $read ) )
    		{
    			if( ( $len = gzwrite( $write, fgets( $read, 4096 ) ) ) )
    			{
    				$written += $len ;
    				
    				printf("\r\r%d bytes", $written );
    			}
    		}
    		fclose( $read );
    		gzclose( $write );
    	}
    	else printf( "Cannot open %s.gz for writing\r\n", YOUR_XML );
    }
    else printf("Cannot open %s for reading\r\n", YOUR_XML );
    ?>
    
    PHP:
    Save the above code ( in the same folder as your xml file ) as compress.php, before you save it set YOUR_XML to the name of the file you want to compress.

    Open a command prompt and type "cd C:\my working directory" where "C:\my working directory" is the path of your file and compress.php

    Type "php compress.php" and wait for execution to finish.

    Now upload {YOUR_XML}.gz found in the same folder as YOUR_XML to your webserver into a directory that the server has write permissions for - if you dunno what that is, upload to a new directory that you create for this purpose and chmod the dir to 777 with your FTP client.

    Now copy and paste the following into a file named uncompress.php

    
    <?php
    /**
     * @author Interviolet
     * @package [$package]
     * @filename [$filename]
     * @copyright 2007
     */
    
    if( !defined( "YOUR_GZ" ) )		define("YOUR_GZ",		"Untitled-1.psd.gz" );
    
    if( ( $read = gzopen( YOUR_GZ, 'r' ) ) )
    {
    	if( ( $write = fopen( preg_replace( '~\.gz$~', '', YOUR_GZ ), 'w' ) ) )
    	{
    		while( !feof( $read ) )
    		{
    			if( ( $len = fwrite( $write, gzread( $read, 4096 ) ) ) )
    			{
    				$written += $len ;
    				printf("\r\r%d bytes", $written );
    			}
    		}
    	}
    	else printf( "Cannot open %s for writing\r\n", preg_replace( '~\.gz$~', '', YOUR_GZ ) );
    }
    else printf("Cannot open %s for reading", YOUR_GZ );
    ?>
    
    PHP:
    Upload to the directory you just chmodded to 777 and visit http://yoursite/your_chmodded_directory/uncompress.php

    That'll work ... gzcompression can work wonders with text files ... Untitled-1.psd was 257kb and is 28kb compressed and that's binary format ....
     
    krakjoe, Oct 24, 2007 IP