Categories
Code krpano

Compress XML for your krpano projects.

Today I wrote a little script to remove unnescessary bytes from an xml. Feel free to make it even better.

Create a file called tourbuilder.php and paste this code inside of it

<?php
class Tourbuilder{
    public function compressXML($output){
        //strip comments
        $output = preg_replace('/<!--(.*?)-->/', '', $output);
        //strip tabs, double spaces and newlines
        $output = preg_replace("/\s+/S", " ", $output);
        //strip spaces between tags
        $output = str_replace("> <", "><", $output);
        return $output;
    }
    public function compressXMLFile($filepath){
        $file = file_get_contents($filepath);
        return $this->compressXML($file);
    }
}
?>

Next, create file called parser.php and paste this code:

<?php
    #load the tourbuilder class
    require_once('tourbuilder.php');
    $builder = new Tourbuilder();
    #set xml headers so the browser recognizes this as xml
    header ("Content-Type:text/xml");
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
    echo $builder->compressXMLFile('tour.xml');
    #you could echo another xml if you want to
    exit;
?>

Now copy paste those two files in your tour directory and make sure you have the correct path to the xml set. if tour.xml doesn’t work, try dirname(‘tour.xml’) or complete path. Make sure there is no code echoed or printed before the headers. And finally make sure your server supports file_get_contents. Some (cheap) shared hosting disallow it (You could always setup a local server using WAMP or LAMP).

Next redirect your browser to yourdomain.com/thetour/parser.php If everything is setup ok, you’ll see a compressed xml (use view source, since some browser auto indent the code). Now you can save the xml, and upload it (If there is enough animo, I’ll create an option so it will save automatically to tour.min.xml or something).

Never link the parser, as it will generate the xml every visit. This is slow and the user has to redownload the xml everytime (since its not cached). It’s purely made to generate an xml once, and then save it.