Public Member Functions | Data Fields

XML2SQL Class Reference

Class for importing XML documents into a relational database. More...

Public Member Functions

 XML2SQL ($a_dbconnection, $a_xmltree, $a_version="1.0", $a_encoding="UTF-8", $a_charset="UTF-8")
 constructor init db-handler
 insertDocument ()
 insertXMLObject ()
 insertNode ($a_node)
 updateNode ($a_node)
 insertNodeData ($a_node)
 insertElement ($a_node)
 insertElement private
 insertText ($a_node)
 insertText private
 insertComment ($a_node)
 insertComment private
 insertAttributes ($a_node)
 insertAttributes private
 getEntryId ($a_table, $a_column, $a_return_value, $a_value)
 getEntryId checks if a single value exists in database private
 getLastInsertId ()
 getLastInsertId private
 prepareData ($a_data)
 prepare db insertion with addslashes() private
 insertStructureIntoTree ($a_nodes, $a_id)

Data Fields

 $obj_id
 $mapping
 $version
 $encoding
 $charset
 $db
 $xmltree
 The DOM XML representation.

Detailed Description

Class for importing XML documents into a relational database.

Author:
Sascha Hofmann <shofmann@databay.de>
Helmut Schottmüller <hschottm@tzi.de>
Version:
Id:
class.XML2SQL.php 4949 2004-09-14 19:13:58Z hschottm

Definition at line 32 of file class.XML2SQL.php.


Member Function Documentation

XML2SQL::getEntryId ( a_table,
a_column,
a_return_value,
a_value 
)

getEntryId checks if a single value exists in database private

Parameters:
string db table name
string table column
string value you seek
Returns:
boolean true when value exists

Definition at line 305 of file class.XML2SQL.php.

References $q, $res, and $row.

Referenced by insertAttributes(), and insertElement().

        {
                $q = "SELECT DISTINCT ".$a_return_value." FROM ".$a_table." ".
                         "WHERE ".$a_column."='".$a_value."'";
                $res = $this->db->query($q, DB_FETCHMODE_ASSOC);
                if ($res->numRows() == 0)
                {
                        return false;
                }

                $row = $res->fetchRow();
                return $row[0];
        }

Here is the caller graph for this function:

XML2SQL::getLastInsertId (  ) 

getLastInsertId private

Returns:
integer

Definition at line 324 of file class.XML2SQL.php.

References $q, $res, and $row.

Referenced by insertAttributes(), insertElement(), insertNode(), and insertXMLObject().

        {
                $q = "SELECT LAST_INSERT_ID()";
                $res = $this->db->query($q);
                $row = $res->fetchRow();
                return $row[0];
        }       

Here is the caller graph for this function:

XML2SQL::insertAttributes ( a_node  ) 

insertAttributes private

Parameters:
array node data
Returns:
boolean

Definition at line 253 of file class.XML2SQL.php.

References $q, getEntryId(), and getLastInsertId().

Referenced by insertNodeData().

        {
                if (is_array($a_node["attr_list"]))
                {
                        foreach ($a_node["attr_list"] as $attr => $value)
                        {
                                $attribute_id = $this->getEntryId("xml_attribute_name","attribute","attribute_id",$attr);

                                // insert attribute first if it does not exist
                                if ($attribute_id == false)
                                {
                                        $q = "INSERT INTO xml_attribute_name (attribute) ".
                                                 "VALUES ('".$attr."')";
                                        $this->db->query($q);
                        
                                        $attribute_id = $this->getLastInsertId();
                                }

                                //$value_id = $this->getEntryId("xml_attribute_value","value","value_id",$value);

                                // insert attribute value first if it does not exist
                                //if ($value_id == false)
                                //{
                                        $q = "INSERT INTO xml_attribute_value (value) ".
                                                 "VALUES ('".$value."')";
                                        $this->db->query($q);
                        
                                        $value_id = $this->getLastInsertId();
                                //}

                                // create reference entry
                                $q = "INSERT INTO xml_attribute_idx (node_id,attribute_id,value_id) ".
                                         "VALUES ".
                                         "('".$a_node["node"]."','".$attribute_id."','".$value_id."')";
                                $this->db->query($q);
                        }
                        
                        return true;
                }
                
                return false;
        }

Here is the call graph for this function:

Here is the caller graph for this function:

XML2SQL::insertComment ( a_node  ) 

insertComment private

Parameters:
array node data

Definition at line 238 of file class.XML2SQL.php.

References $q.

Referenced by insertNodeData().

        {
                $q = "INSERT INTO xml_comment ".
                         "(node_id,comment) ".
                         "VALUES ".
                         "('".$a_node["node"]."','".$a_node["content"]."')";
                $this->db->query($q);
        } 

Here is the caller graph for this function:

XML2SQL::insertDocument (  ) 

Definition at line 83 of file class.XML2SQL.php.

References $id, exit, insertNode(), insertNodeData(), insertXMLObject(), and updateNode().

        { 
                $this->obj_id = $this->insertXMLObject();
                if (!$this->obj_id) 
                {
                        print "There was an error writing the xml file to the database!";
                        exit();
                }
                // insert basic structure of document
                foreach ($this->xmltree as $id => $node) {
                        $node_id = $this->insertNode($node);
                        $this->mapping[$id] = $node_id;
                } 
                // re-map node_ids
                foreach ($this->xmltree as $id => $node) {
                        $this->xmltree[$id]["parent"] = $this->mapping[$node["parent"]];
                        $this->xmltree[$id]["prev"] = $this->mapping[$node["prev"]];
                        $this->xmltree[$id]["next"] = $this->mapping[$node["next"]];
                        $this->xmltree[$id]["first"] = $this->mapping[$node["first"]];
                        $this->xmltree[$id]["node"] = $this->mapping[$id];
                } 

                foreach ($this->xmltree as $id => $node) {
                        $this->updateNode($node);
                        $this->insertNodeData($node);
                } 

                return $this->xmltree;
        } 

Here is the call graph for this function:

XML2SQL::insertElement ( a_node  ) 

insertElement private

Parameters:
array node data

Definition at line 196 of file class.XML2SQL.php.

References $q, getEntryId(), and getLastInsertId().

Referenced by insertNodeData().

        {
                $element_id = $this->getEntryId("xml_element_name","element","element_id",$a_node["name"]);
                
                // insert element first if it does not exist
                if ($element_id == false)
                {
                        $q = "INSERT INTO xml_element_name (element) ".
                                 "VALUES ('".$a_node["name"]."')";
                        $this->db->query($q);
                        
                        $element_id = $this->getLastInsertId();
                }
                
                // create reference entry
                $q = "INSERT INTO xml_element_idx (node_id,element_id) ".
                         "VALUES ('".$a_node["node"]."','".$element_id."')";
                $this->db->query($q);
        } 

Here is the call graph for this function:

Here is the caller graph for this function:

XML2SQL::insertNode ( a_node  ) 

Definition at line 123 of file class.XML2SQL.php.

References $q, and getLastInsertId().

Referenced by insertDocument().

        {
                $q = "INSERT INTO xml_tree ".
                         "(xml_id,lft,rgt,node_type_id,depth,struct) ".
                         "VALUES ".
                         "('".$this->obj_id."','".$a_node["left"].
                         "','".$a_node["right"]."','".$a_node["type"].
                         "','".$a_node["depth"]."','".$a_node["struct"]."') ";
                $this->db->query($q);

                return $this->getLastInsertId();
        } 

Here is the call graph for this function:

Here is the caller graph for this function:

XML2SQL::insertNodeData ( a_node  ) 

Definition at line 148 of file class.XML2SQL.php.

References insertAttributes(), insertComment(), insertElement(), insertText(), and prepareData().

Referenced by insertDocument().

        {
                //echo "<PRE>";echo var_dump($a_node);echo "</PRE>";
                $a_node = $this->prepareData($a_node);
                
                //echo "<PRE>";echo var_dump($a_node);echo "</PRE>";

                switch ($a_node["type"]) {
                        case 1:
                                $this->insertElement($a_node);
                                $this->insertAttributes($a_node);
                                break;

                        case 3:
                                $this->insertText($a_node);
                                break;

                        case 4: 
                                // $this->insertCData($a_node);
                                break;

                        case 5: 
                                // $this->insertEntityRef($a_node);
                                break;

                        case 6: 
                                // $this->insertEntity($a_node);
                                break;

                        case 7: 
                                // $this->insertPI($a_node);
                                break;

                        case 8:
                                $this->insertComment($a_node);
                                break;

                        default: 
                                // nix
                                break;
                } // switch
        } 

Here is the call graph for this function:

Here is the caller graph for this function:

XML2SQL::insertStructureIntoTree ( a_nodes,
a_id 
)

Definition at line 353 of file class.XML2SQL.php.

        {
                // init tree
                $lm_tree = new Tree($a_id,$a_id);
                
                //prepare array and kick all nodes with no children
                foreach ($a_nodes as $key => $nodes)
                {
                        if (!is_array($nodes[key($nodes)]))
                        {
                                array_splice($a_nodes,$key);
                                break;
                        }
                }

                // insert first_node
                $parent_id = $a_id;
                $lm_tree->insertNode(key($a_nodes[0]),$parent_id,0);
                
                // traverse array to build tree structure by inserting nodes to db-table tree
                foreach ($a_nodes as $key => $nodes)
                {
                        $parent_parent_id = $parent_id;
                        $parent_id = key($nodes);

                        foreach (array_reverse($nodes[$parent_id]) as $child_id)
                        {
                                $lm_tree->insertNode($child_id,$parent_id,$parent_parent_id);
                        }
                }
        }

XML2SQL::insertText ( a_node  ) 

insertText private

Parameters:
array node data

Definition at line 221 of file class.XML2SQL.php.

References $q.

Referenced by insertNodeData().

        {
                // klappt nicht, weil die spaces maskiert sind :-(
                $content = $a_node["content"];
        
                $q = "INSERT INTO xml_text ".
                         "(node_id,textnode) ".
                         "VALUES ".
                         "('".$a_node["node"]."','".$content."')";
                $this->db->query($q);
        } 

Here is the caller graph for this function:

XML2SQL::insertXMLObject (  ) 

Definition at line 113 of file class.XML2SQL.php.

References $q, $result, and getLastInsertId().

Referenced by insertDocument().

                                   {
                $q = sprintf("INSERT INTO xml_object (ID, version, encoding, charset, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
                        $this->db->quote($this->version),
                        $this->db->quote($this->encoding),
                        $this->db->quote($this->charset)
                );
                $result = $this->db->query($q);
                return $this->getLastInsertId();
        }

Here is the call graph for this function:

Here is the caller graph for this function:

XML2SQL::prepareData ( a_data  ) 

prepare db insertion with addslashes() private

Parameters:
array 
Returns:
arrayr

Definition at line 338 of file class.XML2SQL.php.

References $data.

Referenced by insertNodeData().

        {
                foreach ($a_data as $key => $value)
                {
                        if (is_string($value))
                                $data[$key] = addslashes($value);
                        else
                                $data[$key] = $value;                   
                }
                
                return $data;
        }

Here is the caller graph for this function:

XML2SQL::updateNode ( a_node  ) 

Definition at line 136 of file class.XML2SQL.php.

References $q.

Referenced by insertDocument().

        {
                $q = "UPDATE xml_tree SET ".
                         "parent_node_id = '".$a_node["parent"]."',".
                         "prev_sibling_node_id = '".$a_node["prev"]."',".
                         "next_sibling_node_id = '".$a_node["next"]."',".
                         "first_child_node_id = '".$a_node["first"]."' ".
                         "WHERE node_id = '".$a_node["node"]."' ".
                         "AND xml_id = '".$this->obj_id."'";
                $this->db->query($q);
        } 

Here is the caller graph for this function:

XML2SQL::XML2SQL ( a_dbconnection,
a_xmltree,
a_version = "1.0",
a_encoding = "UTF-8",
a_charset = "UTF-8" 
)

constructor init db-handler

public

Definition at line 74 of file class.XML2SQL.php.

        {
                $this->xmltree = $a_xmltree;
                $this->db = $a_dbconnection;
                $this->version = $a_version;
                $this->encoding = $a_encoding;
                $this->charset = $a_charset;
        }


Field Documentation

XML2SQL::$charset

Definition at line 51 of file class.XML2SQL.php.

XML2SQL::$db

Definition at line 58 of file class.XML2SQL.php.

XML2SQL::$encoding

Definition at line 50 of file class.XML2SQL.php.

XML2SQL::$mapping

Definition at line 48 of file class.XML2SQL.php.

XML2SQL::$obj_id

Definition at line 40 of file class.XML2SQL.php.

XML2SQL::$version

Definition at line 49 of file class.XML2SQL.php.

object XML2SQL::$xmltree

The DOM XML representation.

public

Definition at line 66 of file class.XML2SQL.php.


The documentation for this class was generated from the following file: