This class provides a nested set wrapper for DOM XML trees. More...
Public Member Functions | |
XMLNestedSet () | |
Constructor init XMLNestedSet handler You may initiate this class with an existing domDocument. | |
_XMLNestedSet () | |
loadDocument ($a_filename, $a_validate=false) | |
Loads a XML document from a file and builds a DOM representation in $this->doc. | |
setEncoding ($a_encode, $a_overwrite=false) | |
Set the encoding of the DOM XML document. | |
getEncoding () | |
Get the encoding of the DOM XML document. | |
setCharset ($a_charset, $a_overwrite=false) | |
Set the charset of the DOM XML document. | |
getCharset () | |
Get the charset of the DOM XML document. | |
transform ($node, $left2=-1, $lvl=0) | |
Creates a nested set transformation of a DOM XML document node. | |
& | buildTree ($a_node="") |
Wrapper for $this->transform defaults to $this->doc if no node given and returns $this->nestedset. | |
trim ($a_node) | |
Traverse domDocument and removes all useless nodes that are created due to whitespaces in the source file. | |
trimDocument ($a_node= '') | |
Wrapper for $this->trim defaults to $this->doc if no node given and returns cleaned dom document. | |
dumpDocument ($a_stdout=-1, $a_compress=false, $a_format=false) | |
wrapper for dump_mem() and dump_file() converts the entire DOM tree in $this->doc to a string optional: writes the string to a file if path & filename is specified. | |
getTextFromElement ($a_element) | |
Fetch all text parts from an element even when the text is interupted by another element example: <paragraph>This text ispart of the same element. | |
isLeafElement ($a_node, $a_elementname, $a_num=0) | |
Find leaf elements. | |
getElementsByTagname ($a_elementname, $a_node="") | |
Wrapper for get_elements_by_tagname searches domDocument for specified elementname, starting at $a_node. | |
getRoot () | |
Wrapper for document_element Main purpose of this method is to simplify access of DOM-Functions. | |
getElementId ($a_node) | |
Get internal reference id of a domNode. | |
getElementName ($a_node) | |
Get node_name. | |
save_to_db ($db) | |
loadFromDb ($db, $xml_object_id) | |
delete_from_db ($db, $xml_object_id) | |
Data Fields | |
$doc | |
$nestedset | |
$error |
This class provides a nested set wrapper for DOM XML trees.
XMLNestedSet reads xml files, creates a DOM XML tree and creates a nested set transformation for that tree.
class.XMLNestedSet.php Assessment
Definition at line 39 of file class.XMLNestedSet.php.
XMLNestedSet::_XMLNestedSet | ( | ) |
Definition at line 86 of file class.XMLNestedSet.php.
{
if ($this->doc) {
$this->doc->free();
}
}
& XMLNestedSet::buildTree | ( | $ | a_node = "" |
) |
Wrapper for $this->transform defaults to $this->doc if no node given and returns $this->nestedset.
object | domNode |
Definition at line 337 of file class.XMLNestedSet.php.
References transform().
Referenced by save_to_db().
{ $this->nestedset = array(); if (empty($a_node)) { $a_node = $this->doc; } $this->transform($a_node, 1); return $this->nestedset; }
XMLNestedSet::delete_from_db | ( | $ | db, | |
$ | xml_object_id | |||
) |
Definition at line 557 of file class.XMLNestedSet.php.
References $q, $result, and $row.
{ $tables = array( "xml_element_namespace", "xml_pi_target", "xml_pi_data", "xml_cdata", "xml_entity_reference", "xml_attribute_namespace", "xml_text", "xml_comment", "xml_attribute_idx", "xml_element_idx" ); foreach ($tables as $table) { $q = sprintf ("SELECT $table.* FROM $table, xml_tree WHERE $table.node_id = xml_tree.node_id AND xml_tree.xml_id = %s", $db->quote("$xml_object_id") ); $result = $db->query($q); if ($result->numRows()) { while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT)) { $q_delete = sprintf ("DELETE FROM $table WHERE node_id = %s", $db->quote($row->node_id) ); $result_delete = $db->query($q_delete); if (strcmp($table, "xml_attribute_idx") == 0) { $q_delete = sprintf ("DELETE FROM xml_attribute_value WHERE value_id = %s", $db->quote($row->value_id) ); $result_delete = $db->query($q_delete); $q_delete = sprintf ("DELETE FROM xml_attribute_name WHERE attribute_id = %s", $db->quote($row->attribute_id) ); $result_delete = $db->query($q_delete); } if (strcmp($table, "xml_element_idx") == 0) { $q_delete = sprintf ("DELETE FROM xml_element_name WHERE element_id = %s", $db->quote($row->element_id) ); $result_delete = $db->query($q_delete); } } } } $q = sprintf("DELETE FROM xml_object WHERE ID=%s", $db->quote($xml_object_id) ); $result = $db->query($q); $q = sprintf("DELETE FROM xml_tree WHERE xml_id = %s", $db->quote($xml_object_id) ); $result = $db->query($q); }
XMLNestedSet::dumpDocument | ( | $ | a_stdout = -1 , |
|
$ | a_compress = false , |
|||
$ | a_format = false | |||
) |
wrapper for dump_mem() and dump_file() converts the entire DOM tree in $this->doc to a string optional: writes the string to a file if path & filename is specified.
Otherwise only the string is returned.
string | path/filename | |
boolean | compress XML content | |
boolean | format XML content with whitespaces |
Definition at line 404 of file class.XMLNestedSet.php.
{ if ($a_stdout != -1) { $this->doc->dump_file($a_stdout,$a_compress,$a_format); } return $this->doc->dump_mem(); }
XMLNestedSet::getCharset | ( | ) |
Get the charset of the DOM XML document.
Get the charset of the DOM XML document
Definition at line 202 of file class.XMLNestedSet.php.
Referenced by save_to_db().
{
return $this->doc->charset;
}
XMLNestedSet::getElementId | ( | $ | a_node | ) |
Get internal reference id of a domNode.
object | domNode |
Definition at line 516 of file class.XMLNestedSet.php.
{
$node = (array) $a_node;
return $node[0];
}
XMLNestedSet::getElementName | ( | $ | a_node | ) |
Get node_name.
object | domNode |
Definition at line 529 of file class.XMLNestedSet.php.
{
return $a_node->node_name();
}
XMLNestedSet::getElementsByTagname | ( | $ | a_elementname, | |
$ | a_node = "" | |||
) |
Wrapper for get_elements_by_tagname searches domDocument for specified elementname, starting at $a_node.
If no node was given searches the entire domDocument in $this->doc returns an array of all nodes found
string | tagname of element | |
object | domNode where to start searching (optional) |
Definition at line 484 of file class.XMLNestedSet.php.
{ if (empty($a_node)) { $a_node = $this->doc; } if (count($node = $a_node->get_elements_by_tagname($a_elementname)) > 0) { return $node; } return false; }
XMLNestedSet::getEncoding | ( | ) |
Get the encoding of the DOM XML document.
Get the encoding of the DOM XML document
Definition at line 168 of file class.XMLNestedSet.php.
Referenced by save_to_db().
{
return $this->doc->encoding;
}
XMLNestedSet::getRoot | ( | ) |
Wrapper for document_element Main purpose of this method is to simplify access of DOM-Functions.
Definition at line 504 of file class.XMLNestedSet.php.
{
return $this->doc->document_element();
}
XMLNestedSet::getTextFromElement | ( | $ | a_element | ) |
Fetch all text parts from an element even when the text is interupted by another element example: <paragraph>This text ispart of the same element.
</paragraph> This method returns: This text is part of the same element.
object | dom node of type ELEMENT (id:1) |
Definition at line 425 of file class.XMLNestedSet.php.
References trim().
{ if ($a_element->node_type() == XML_ELEMENT_NODE) { $value = ""; foreach ($a_element->child_nodes() as $child) { if ($child->node_type() == XML_TEXT_NODE) { $value .= $child->content; } } return trim($value); } die("<b>".$a_element."</b> is not a valid element node!"); }
XMLNestedSet::isLeafElement | ( | $ | a_node, | |
$ | a_elementname, | |||
$ | a_num = 0 | |||
) |
Find leaf elements.
In this context leaf elements are defined as elements that don't contain other elements of the same type!
object | domNode of type ELEMENT | |
string | tagname your are looking for | |
integer | auxilliary variable to avoid counting start node itself. must set to 1 for the first function call |
Definition at line 452 of file class.XMLNestedSet.php.
{ $var = true; if ($childs = $a_node->child_nodes()) { foreach ($childs as $child) { $var = $this->isLeafElement($child, $a_elementname); if (!$var) { return false; } } } if (($a_node->node_type() == XML_ELEMENT_NODE) && ($a_node->tagname == $a_elementname) && ($a_num != 1)) { return false; } return $var; }
XMLNestedSet::loadDocument | ( | $ | a_filename, | |
$ | a_validate = false | |||
) |
Loads a XML document from a file and builds a DOM representation in $this->doc.
The xml-document is parsed automatically. You may also validate it against a DTD by setting the second parameter to 'true'
string | Complete path to the xml file | |
boolean | set mode: parsing (false,default) or validating (true) public |
Definition at line 103 of file class.XMLNestedSet.php.
References $error, domxml_open_file(), exit, setCharset(), and setEncoding().
{ if ($a_validate) { $mode = DOMXML_LOAD_VALIDATING; } else { $mode = DOMXML_LOAD_PARSING; } if (!file_exists($a_filename)) { array_push($this->error, array( "line" => "", "errormessage" => "The file $a_filename does not exist!" )); return; } $this->doc = domxml_open_file($a_filename, $mode, $this->error); // stop parsing if an error occured if ($this->error) { $error_msg = "Error(s) while parsing the document!<br><br>"; foreach ($this->error as $error) { $error_msg .= $error["errormessage"]." in line: ".$error["line"]."<br>"; } print $error_msg; exit(); } // set encoding to UTF-8 if empty $this->setEncoding("iso-8859-1", true); // set charset to UTF-8 $this->setCharset("iso-8859-1"); return $this->doc; }
XMLNestedSet::loadFromDb | ( | $ | db, | |
$ | xml_object_id | |||
) |
XMLNestedSet::save_to_db | ( | $ | db | ) |
Definition at line 534 of file class.XMLNestedSet.php.
References buildTree(), getCharset(), and getEncoding().
{ if (empty($this->doc)) { return; } if (empty($this->nestedset)) { $this->buildTree(); } $xml2sql = new XML2SQL($db, $this->nestedset, "1.0", $this->getEncoding(), $this->getCharset()); $xml2sql->insertDocument(); unset($xml2sql); }
XMLNestedSet::setCharset | ( | $ | a_charset, | |
$ | a_overwrite = false | |||
) |
Set the charset of the DOM XML document.
Set the charset of the DOM XML document. If the document has a standard charset you have to force that the charset will be overwritten.
string | charset | |
boolean | overwrite existing charset (true) or not (false) |
Definition at line 184 of file class.XMLNestedSet.php.
Referenced by loadDocument().
{ if (is_integer($this->doc->charset) or ($a_overwrite)) { $this->doc->charset = $a_charset; return true; } return false; }
XMLNestedSet::setEncoding | ( | $ | a_encode, | |
$ | a_overwrite = false | |||
) |
Set the encoding of the DOM XML document.
Set the encoding of the DOM XML document. If the document has a standard encoding you have to force that the encoding will be overwritten.
string | encoding charset | |
boolean | overwrite existing encoding charset (true) or not (false) |
Definition at line 151 of file class.XMLNestedSet.php.
Referenced by loadDocument().
{ if (empty($this->doc->encoding) or ($a_overwrite)) { $this->doc->encoding = $a_encode; return true; } return false; }
XMLNestedSet::transform | ( | $ | node, | |
$ | left2 = -1 , |
|||
$ | lvl = 0 | |||
) |
Creates a nested set transformation of a DOM XML document node.
Creates a nested set transformation of a DOM XML document node and calles the function recursive for all child nodes. The following structure informations will be stored: $arr[id] = array( content (str) = node_value (only text nodes have a value) name (str) = tagname or entityname type (int) = element_type_id depth (int) = depth of node in tree parent (int) = id of parent node first (int) = id of first child node prev (int) = id of previous sibling node next (int) = id of next sibling node left (int) = left value (for traversing tree in relational DB) right (int) = right value (for traversing tree in relational DB)) The key is the internal id of the DOM XML document. Also the ids of other nodes are internal references. The array is written to $this->nestedset. Use $this->buildTree() to return the variable.
object | domNode | |
integer | left value (optional; only needed for recursion)) | |
integer | depth of node in tree (optional, default is 0) private |
Definition at line 233 of file class.XMLNestedSet.php.
References $data, $left, and trim().
Referenced by buildTree().
{ // Static varialbe for the nested set counter static $left; // set depth $lvl++; // start value given from outside? if ($left2 > 0) { $left = $left2; } // set default value 1 if no value given if (!$left) { $left = 1; } // convert DomNode object to an array $node2 = (array)$node; // init structure data // provides additional information about document structure // bitwise: // 1: has attributes // 2: has text element $this->nestedset[$node2[0]]["struct"] = 0; if ($parent = $node->parent_node()) { $parent = (array)$parent; } if ($first = $node->first_child()) { $first = (array)$first; } if ($prev = $node->previous_sibling()) { $prev = (array)$prev; } if ($next = $node->next_sibling()) { $next = (array)$next; } $this->nestedset[$node2[0]]["content"] = trim($node->node_value()); $this->nestedset[$node2[0]]["name"] = $node->node_name(); $this->nestedset[$node2[0]]["type"] = $node->type; $this->nestedset[$node2[0]]["depth"] = $lvl; $this->nestedset[$node2[0]]["parent"] = $parent[0]; $this->nestedset[$node2[0]]["first"] = $first[0]; $this->nestedset[$node2[0]]["prev"] = $prev[0]; $this->nestedset[$node2[0]]["next"] = $next[0]; $this->nestedset[$node2[0]]["left"] = $left; // increase nested set counter for the next node $left++; // write attributes to sub-array if ($node->has_attributes()) { $data = ""; foreach ($node->attributes() as $attribute) { $data[$attribute->name] = $attribute->value; } $this->nestedset[$node2[0]]["attr_list"] = $data; $this->nestedset[$node2[0]]["struct"] += 1; } // check if one child is a text_node foreach ($node->child_nodes() as $child) { if ($child->node_type() == XML_TEXT_NODE) { $this->nestedset[$node2[0]]["struct"] += 2; break; } } // recursive call foreach ($node->child_nodes() as $child) { $this->transform($child, $left, $lvl); } $this->nestedset[$node2[0]]["right"] = $left; $left++; }
XMLNestedSet::trim | ( | $ | a_node | ) |
Traverse domDocument and removes all useless nodes that are created due to whitespaces in the source file.
object | domNode private |
Definition at line 357 of file class.XMLNestedSet.php.
Referenced by getTextFromElement(), transform(), and trimDocument().
{ if ($a_node->has_child_nodes()) { $childs = $a_node->child_nodes(); foreach ($childs as $child) { $content = trim($child->get_content()); if (empty($content) and ($child->type == XML_TEXT_NODE)) { $child->unlink_node(); } else { $this->trim($child); } } } }
XMLNestedSet::trimDocument | ( | $ | a_node = '' |
) |
Wrapper for $this->trim defaults to $this->doc if no node given and returns cleaned dom document.
object | domNode (optional) public |
Definition at line 382 of file class.XMLNestedSet.php.
References trim().
{ if (empty($a_node)) { $a_node = $this->doc; } $this->trim($a_node); return $a_node; }
XMLNestedSet::XMLNestedSet | ( | ) |
Constructor init XMLNestedSet handler You may initiate this class with an existing domDocument.
object | domDocument public |
Definition at line 73 of file class.XMLNestedSet.php.
XMLNestedSet::$doc |
Definition at line 47 of file class.XMLNestedSet.php.
XMLNestedSet::$error |
Definition at line 64 of file class.XMLNestedSet.php.
Referenced by loadDocument().
XMLNestedSet::$nestedset |
Definition at line 55 of file class.XMLNestedSet.php.