myXTree is an interface for SQL tree. In myXTree there is used model of the embedded multitude with new parameter debth.
That class is enough simple and doesn�t require a lot of coding. As an input data there is used DOM document. Transfer DOM document to the class and it saves it in the SQL tree. For data fetch from the SQL tree there are used XPath expressions.
We use DOM document because it has a tree-type structure. There are several ways of getting the DOM document. First way is API; with its help DOM structure can be created. Second way is XML document; if you parse it you can get DOM. Tthe result of XSLT processor transformation is also a DOM document. Thus the DOM document is a heart of the XML technology.
For working with class you have to know XPath language and understand the differences between DOM document and SQL tree.
Data adding to the SQL tree.

It is one of the simplest operations. At first you have to fulfill some preparatory operations:
<?php
// myDOM ia a part of myXML packet
require_once("myDOM/myDOM.php");
// myXTree
require_once("myXTree.php");
// abstract acces to the databases from the PEAR repository
require_once("DB.php");

$dom = new Document;
$dom->appendChild($dom->createElement("root"));

// connect to the database
$db = DB::connect("mysql://$user:$pass@$host/$name");
// variables $user, $pass etc.
$xtree =& myXTree::create($db, $prefix);
PEAR::isError($xtree) and
trigger_error($xtree->getMessage(), E_USER_ERROR);
?>
Now everything is prepared for the next operations.
So, adding. insert() method has three parameters. First parameter is $node; it is objects ELEMENT_NODE, TEXT_NODE, CDATA_SECTION_NODE and ATTRIBUTE_NODE
Second parameter indicates the object that should add object $node, pointed in the first parameter. As only ELEMENT_NODE can have children then only these objects can be transferred to the $parent parameter. More than that $parent parameter should exist in the database; otherwise there will appear an error message. How can we create the Element class object and set up a correspondence between that object and record in the database.
myXTree creates the Element class object and sets up a correspondence between that object and record in the database itself. myXTree will fulfill that operation if you call the method select().
Here you can ask another question: �Where can I get the $parent object if my database is empty?� The answer is very simple, your database can�t be empty because it has root. If $parent isn�t set $node is added to the SQL tree�s root.
Third parameter is $deep. It points at necessity of adding all the children of the $node object.
Let�s view our example:
<?php
// as a first $node parameter transfer the object $dom->documentElement, but not
// $dom, because $dom has DOCUMENT_NODE type.
$result = $xtree->insert(&$dom->documentElement, $parent = null, $deep = true);
PEAR::isError($result)) and
trigger_error($result->getMessage(), E_USER_ERROR);
?>
Of course we could insert any part of the DOM document instead of $dom->documentElement.
Data deleting

delete() method has only one parameter $node that should be deleted. More than that if $node has children in database they should be deleted too. Object should exist in the database and it can be got by means of method select().
<?php
// Delete for example $dom->documentElement.
$result = $xtree->delete(&$dom->documentElement);
PEAR::isError($result) and
trigger_error($result->getMessage(), E_USER_ERROR);
?>
Fetch data

For SQL tree data fetch you have to use select() method. First parameter of that method is a line � Xpath expression. Second parameter indicates the method of XPath expression processing. Select() method returns the array of references to the Node objects.
At first we have to understand the difference between XML document and DOM document. XML document is a formatted text. DOM document is a tree-type structure of the objects in the memory. Every object in the DOM document structure has references to the next objects: previousSabling, nextSibling, firstChild, lastChild etc.
<root>
<node-1-1>
<node-1-2>
Simple Text
</node-1-2>
<node-2-2>
<node-1-3>
Very Simple Text
</node-1-3>
</node-2-2>
</node-1-1>
</root>
In that example previousSabling for node-2-2 is node-1-2, node-2-2 is a nextSibling for node-1-2. firstChild for node-1-1 is node-1-2, lastChild is node-2-2. "Simple Text" node is a firstChild and lastChild for node-1-2.
What will XPath processor do if we ask it to find nodes that have child like TEXT_NODE ("//node()[text()]"). It will look through the tree and returns node-1-2 and node-1-3 as a reference array.
What does the XPath processor of the myXTree class do? It compiles the XPath expressions to the SQL query, executes it and gets an array with database records. According to that records it will create objects such as ELEMENT_NODE, TEXT_NODE etc. You have to restore child-parent relations, i.e. restore the object tree. It can be done with the help of method select(). Here is the main difference of XPath expressions using for DOM document and SQL tree.
The main reason is that select() method restores only those files that has been returned by query. For example, "//node()[text()]" returns only node-1-2 and node-1-3. More than that "Simple Text" and "Very Simple Text" are the text nodes that are not returned by "//node()[text()]" as well. That�s why XPath expressions using for SQL tree differs from DOM tree.
Here is an example:
<?php
$resultNode
=& $dom->createElement("resultNode");
$xtree->setReceptionNode(&$resultNode);
$nodeSet = $xtree->select("//node-1-1//");
PEAR::isError($nodeSet) and
trigger_error($nodeSet->getMessage(), E_USER_ERROR);
?>
As a result $resultNode includes node-1-1 with its children, and $nodeSet includes the list of references to the node-1-1 and its children. According to the requirements you can use first or second way for getting the result.
Data update

Here we�ll compare two XML documents.
Data communication is carried out in the XML format. We have to receive data from the SQL tree, then send to user, and then put the received changes to the SQL tree. Data sending and receiving will be used in XML format. Let�s forget about SQL tree for a minute and imagine that we have a deal with XML document.
It is our document
<root>
<node-1-1 id="1">
<node-1-2>
Simple Text
</node-1-2>
<node-2-2>
<node-1-3>
Very Simple Text
</node-1-3>
</node-2-2>
</node-1-1>
</root>
It is a part changed by user
<node-1-1 id="1">
<node-1-2>
Unsimple Text
</node-1-2>
</node-1-1>
If we want to make changes we have to set up a correspondence between these XML documents. XML document is a tree, that�s why elements are defined by their position in the tree. That method doesn�t suit us. Element�s name can�t be identifier. For that purpose we can use an attribute. But text nodes and CDATA nodes can�t have any attributes. So, as we have no choice we�ll use �id� attribute unique inside the whole tree. And for identification of text and CDATA nodes we�ll use parent �id� attribute. The reason of such decision is that the elements must have only one child like EXT_NODE or CDATA_SECTION_NODE. There is one more method of element identification by the �id� attribute and element�s name. In that case �id� attribute must be unique only within the indicated name. Given method isn�t realized in myXTree.
So, we have decided WHAT we�ll update, but now we have to decide HOW. The simplest method is to replace the node-1-1 in the XML document by the corresponded one. Let�s name that method as �Full update�. But in our XML document node-1-1 has a child element node-2-2, thus if we use that update method it will be deleted from the tree. Sometimes we have to update only some part of the node-1-1 children. In that case we have to use a custom update.
update() method use is simple enough. First parameter is a node that should be updated; second parameter shows that given element should be updated with its children; third parameter defines update method: true � full update, false � custom update.
<?php
$result
= $xtree->update(&$dom->documentElement, $deep = true, $full = false);
PEAR::isError($result) and
trigger_error($result->getMessage(), E_USER_ERROR);
Class working

Parameter $recursive for select() method was mentioned above. It�s high time to speak about it.
What is the advantage of SQL tree with embedded multitude model? The main advantage is that one SQL query can get the node with all its children irrespective of the depth. The main disadvantage of the SQL tree is that SQL query creation is enough complicated. There is one way out. You can use XPath language for SQL trees.
For SQL query creation by XPath expression you can use table selfjoin. For example "/root" expression will create SQL query like:
SELECT * FROM Objects, Objects AS Table1, Objects AS Table2 WHERE ...
Table selfjoin means that table will be multiplied by itself. I.e. table with 100 lines will create 1000 lines. There is another method of XPath expressions processing � recursive method.
There were a competition between first (selfjoin) and second (recursive) methods. As a result selfjoin was rehabilitated.
At first there is created a DOM document with 1000 elements in the test. Then that document is saved in the SQL tree and by means of select() method elements fetch is carried out using two methods. As a query there used such expressions as "/*", "/*/*", "/*/*/*". DOM document has 5 nesting levels. In first case every element has one child element; in second case they have 5 elements. In the both cases selfjoin method surpassed recursive method.