PHP Classes

File: sql_xml.php

Recommend this page to a friend!
  Packages of Erik Giberti   sqltoolkit   sql_xml.php   Download  
File: sql_xml.php
Role: Auxiliary script
Content type: text/plain
Description: XML Import Functionality
Class: sqltoolkit
An API for interfacing with databases
Author: By
Last change: bug fix in database_import_xml_url() function
Date: 23 years ago
Size: 3,707 bytes
 

 

Contents

Class file image Download
<?php

/*

    sql_xml.php - PHP functions for importing XML data into SQL toolkit
    Copyright (C) 2003 Erik Giberti (AF-Design), All rights reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

if (!defined("_SQL_XML_FUNCTIONS_")){
    define("_SQL_XML_FUNCTIONS_", TRUE);

    // xml parser for an xml formated dataset converter to create a 
    // database_result class
    
    function database_start_tag_handler($xml_parser, $name, $attributes){
        global $database_import_xml_data_in,     
               $database_import_xml_info_in,
               $database_import_xml_field_in;
        switch ($name){
            case "dataset":
                // assign info settings    
                $database_import_xml_info_in["num_rows"] = $attributes["rows"];
                $database_import_xml_info_in["num_fields"] = $attributes["cols"];
                break;
            case "row":
                // start a new row
                break;
            default:
                // handle a column
                $database_import_xml_field_in = $name;
                break;
        }
    }

function database_end_tag_handler($xml_parser, $name){
    global $database_import_xml_info_in;
    switch ($name){
        case "row":
            $database_import_xml_info_in["current_row"]++;
            break;
        default:
            break;
    }
}

function database_character_handler($xml_parser, $data){
    global $database_import_xml_data_in,
           $database_import_xml_info_in, 
           $database_import_xml_field_in;
        if($database_import_xml_field_in == "" ||
           $database_import_xml_field_in == "dataset" ||
           $database_import_xml_field_in == "row") {
            // swallow this data
        } else {
            // this must be field data
            $database_import_xml_data_in[$database_import_xml_info_in["current_row"]][$database_import_xml_field_in] = $data;
            $database_import_xml_field_in = "";
        }
}

function database_entity_handler($xml_parser, $name, $base, $systemID, $publicID){
    // print "Entity Handler Got: $name";
}

function database_import_xml_packet(&$packet){
    global $database_import_xml_field_in, $database_import_xml_data_in, $database_import_xml_info_in;
    $database_import_xml_info_in["num_rows"] = 0;
    $database_import_xml_info_in["num_fields"] = 0;
    $database_import_xml_info_in["current_row"] = 0;

    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "database_start_tag_handler", "database_end_tag_handler");
    xml_set_character_data_handler($xml_parser, "database_character_handler");
    xml_set_external_entity_ref_handler($xml_parser, "database_entity_handler");
    xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
    xml_parse($xml_parser, $packet) 
        or die ("<b>sql.php</b>: unable to parse xml packet");

    $result = new database_result($xml_parser, "XML");
    $result->assign_external_info($database_import_xml_info_in);
    $result->assign_external_data($database_import_xml_data_in);
    return $result;
} 


function database_import_xml_url($url){
    $handle = fopen ($url, "r"); 
    $packet = fread($handle, 1024);
    fclose($handle);
    return database_import_xml_packet($packet);
}

} // end code lock
?>