Location: PHPKode > projects > XRNS-PHP > xml2mid.php
<?php

// ----------------------------------------------------------------------------
// Variables
// ----------------------------------------------------------------------------

$schema = dirname($_SERVER['SCRIPT_NAME']).'/schemas/midixml.xsd';

// ----------------------------------------------------------------------------
// Requires
// ----------------------------------------------------------------------------

require_once('xrns_functions.php');
require_once('midi_class_v175/classes/midi.class.php');

// ----------------------------------------------------------------------------
// Check User Input
// ----------------------------------------------------------------------------

// get filename component of path
if (isset($argv[0])) $argv[0] = basename($argv[0]);
else $argv[0] = $_SERVER['SCRIPT_NAME'];


if (isset($argc) && $argc < 3) {
    echo "Error: $argv[0] expects at least 2 parameters.\n";
    echo "Usage: `php $argv[0] /path/to/file1.xml file2.mid`\n";
    echo "$argv[0] will output mid file (file2.mid) to current working directory.\n";
    die();
}

if (isset($argv[1]) && isset($argv[2])) {
    if (!file_exists($argv[1])) die("Error: The file $argv[1] was not found.\n");
        $xmlfile = $argv[1];
        $midfile = $argv[2];
} else {
    $xmlfile = "C:\\[bb5entries]\\test\\song3.mid.xml";
    $midfile = "C:\\[bb5entries]\\test\\song3.mid";
}


// ----------------------------------------------------------------------------
// Unpack
// ----------------------------------------------------------------------------

echo "---------------------------------------\n";
echo "$argv[0] is working...\n";
echo date("D M j G:i:s T Y\n");
echo "---------------------------------------\n";

// Load XML
$midixml = simplexml_load_file($xmlfile);
$midixml = $midixml->asXML();

// ----------------------------------------------------------------------------
// Validate
// ----------------------------------------------------------------------------

if (file_exists($schema)) {
    $dd = new DOMDocument;
    $dd->loadXML($midixml);
    if(!$dd->schemaValidate($schema)) {
        echo "Error: XML is invalid!\n";
        die();
    }
    else echo $xmlfile." validated OK!\n";
}
else {
    echo "Warning: $schema not found, skipping XML validation.\n";
}

// Convert text to midi
$midi = new Midi();
$midi->importXML($midixml);
echo "Saving to $midfile...\n";
$midi->saveMidFile($midfile);

echo "---------------------------------------\n";
echo "$argv[0] is done!\n";
echo date("D M j G:i:s T Y\n");
echo "---------------------------------------\n";



?>
Return current item: XRNS-PHP