Application of a class myXTree
The introduction.
myxtree is an interface to sql to a tree. About sql trees it is written much enough (see), therefore it is enough to mention links only, that in myxtree the model of the enclosed sets with addition of one more parameter depth (depth) is used.
The class is simple enough in use in the sense that a lot of coding does not demand. As the entrance data for a class it is used dom the document. Pass dom the document to a class, and he saves it in sql a tree. For sample of the data from sql a tree are used xpath expressions. On an output{exit} of a class again taki receive dom the document.
Why dom the document? Because he too has treelike structure. Because there are some ways to receive dom the document. It is own api with which help it is possible to construct structure dom. It xml the document, proparsiv which, you can receive dom. And also, result of transformation xslt the processor it too dom the document. Thus, dom the document is heart of all technology xml. Therefore has been solved to enter support dom.
However, at simplicity of coding there is a danger to receive not that result which was expected (it similar on regular expressions - not enough code, the big functionality, but while dob`joshsja result!). For job with a class it is necessary to know language xpath and well to understand a difference between dom the document and sql a tree. This clause{article} is called to illuminate these questions.
Addition of the data to sql to a tree.
It is one of the most simple operations. And if to take into account that them only four (addition, change, sample and removal{distance}) that is the whole of 25 %; ~).
For the beginning it is necessary to execute some preparatory operations.
<? php
// mydom is part of a package myxml
require_once (' mydom/mydom.php ');
// Itself myxtree
require_once (' myxtree.php ');
// And abstract access to databases from pear a repository
require_once (' db.php ');
$dom = new document;
// Further $dom it is necessary to fill the document contents one of mentioned
// In the way, for example through dom api
$dom-> appendchild ($dom-> createelement (' root '));
//, etc.
// We create connection to a database
$db = db:: connect (" mysql: // $user: $ pass $ host / $ name ");
// Variables $user, $pass, etc. certainly to substitute yours
// And at last we create object of a class myxtree
$xtree = and myxtree:: create ($db, $prefix);
pear:: iserror ($xtree) and
trigger_error ($xtree-> getmessage (), e_user_error);
// $prefix it is possible to not specify parameter, it is a prefix of names of tables of a database
// The result returned by a method myxtree:: create is better for checking up, suddenly base
// The data is not base myxtree or not that version
?>
Now all is ready for the following operations.
So, addition. The method insert () accepts three parameters. The first parameter $node are objects such as element_node, text_node, cdata_section_node and attribute_node. Other types from the standard dom in a database are not saved. Why? Because yet there was no necessity. And in the second - not all turns out to save (details later).
The second parameter underlines object to which it is necessary to add object $node specified in the first parameter. As only at element_node there can be children accordingly, in parameter $parent it is possible to pass only these objects. Except for that $parent already should exist in a database, the message on a mistake differently will be given. There is a question where to take object of a class element which would be in a database. Will ask more correctly a question how to create object of a class element and to establish conformity between this object and recording in a database. Such production of a question is an essence of job of a class myxtree. He constantly should establish conformity between objects dom the document and recordings in a database. And he needs from you some help (but about it too later). If you will remember this statement it will be much easier to you to work with a class.
And so, myxtree itself will create object of a class element and will establish conformity between this object and recording in a database. He will make it for you if you ask it , having called a method select (). Easier speaking, on xpath to expression you choose (node site necessary for you and receive ready object which substitute in parameter $parent.
But here there is a second lawful question where to take object $parent if a database is still empty. The answer is simple. She is not empty. There already there is a root (root). It{he} always can be chosen on xpath to expression "/", but to do{make} it it is not necessary. By default, if $parent it is not set, $node it is added in a root sql a tree. And such it is possible to add much enough.
And, at last, the third parameter - $deep (it is deep), specifies that that it is necessary to add also all descendants of object $node (i.e. it is possible to add only object $node with attributes, not looking at that that he has descendants).
Let's see, at last, that at us it is done{made} in an example.
<? php
// Well. As I also spoke, it is possible to add directly in a root.
// As the first parameter $node we pass object $dom-> documentelement, and
// Not $dom as $dom has type document_node.
$result = $xtree-> insert (and $dom-> documentelement, $parent = null, $deep = true);
pear:: iserror ($result)) and
trigger_error ($result-> getmessage (), e_user_error);
// We shall necessarily check up success of our action.
?>
Certainly, instead of $dom-> documentelement it would be possible to insert any part of the document dom. Simply at us in an example of her no.
Removal{Distance} of the data.
On logic prophetic now it would be necessary to tell about sample or updating, but removal{distance}, it is following, even more simple operation.
The method delete () accepts only one parameter $node which is necessary for removing. And, if at $node in base there are descendants, they too leave. Certainly, the object should exist in base, and to receive it it is possible also, with the help of a method select ().
<? php
// We shall remove, for example the same $dom-> documentelement. And, as we of it only
// That inserted, conformity between it and recording in a database is already established.
// It I to that it is not necessary to choose it from base a method select ();
$result = $xtree-> delete (and $dom-> documentelement);
pear:: iserror ($result) and
trigger_error ($result-> getmessage (), e_user_error);
Sample of the data.
Here we need good understanding of language xpath.
For sample of the data sql a tree on xpath to expression the method select () is used. The first parameter which accepts this method, is a line - expression xpath. The second parameter specifies a method it is necessary for them to process xpath expression. Purpose of this parameter becomes understandable later. As well as the method evaluate () for dom the document, select () returns a file of links (references) on objects node. But, besides the method select () recreates a tree of objects node. The example will help to understand a difference better.
For the beginning it is necessary to specify a difference between terms xml the document and dom the document. xml the document is definitely formatted text. Thus, below resulted example is xml the document. dom the document is a treelike structure of objects in memory of a computer. I.e. dom the document is used for performance xml the document in memory of a computer. Each object, in structure dom the document has links to the next objects. Namely previoussabling (the previous brother), nextsibling (the subsequent brother), firstchild (the first child), lastchild (last child) and some other.
<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 this example the previous brother for node-2-2 site will be node-1-2 site, and node-2-2 accordingly will be the subsequent brother for node-1-2 site. The first child for node-1-1 site will be node-1-2 site, and the last node-2-2. And the text site " simple text " will be first and last child for node-1-2 site. The tree dom the document is in such a way defined{determined}.
That will make xpath the processor, if we shall ask it to find sites, which have child such as text_node (" // node () [text ()] "). He will be run on a tree and will return to us node-1-2 sites and node-1-3 as a file of links. Pay attention, not copies of objects, and links to them. Not treelike structure, and the list (an one-dimensional file). Having the link to object which is in a tree, we can easily get access to other objects through previoussibling, firstchild, etc.
What will make xpath the processor of a class myxtree? He otkompiliruet xpath expression in sql search, will execute it and will receive a file with recordings a database. On these recordings he will create objects of the certain type (element_node, text_node, etc.) and too will return as the list of links to objects. But, for whom they are necessary in such kind? That they useful it began necessary to restore attitudes{relations} the ancestor - descendant, i.e. to recreate a tree of objects. The method select () does{makes} also it. But, here the big difference also consists in use xpath expressions for dom the document and for sql a tree.
The matter is that the method select () recreates a tree of that has been returned by search. For example, expression " // node () [text ()] " will return only node-1-2 sites and node-1-3. As they are not connected directly with each other tree from them to fail. Besides the text " simple text " and " very simple text " is separate text sites, which too do not come back expression " // node () [text ()] ". Therefore practice of use xpath expressions for sql a tree differs from dom a tree. For example, that text sites have got in result of expression, it is necessary to add it is " // node () [text ()] // ". Thus, the method select () will return the list of links already on four objects and will recreate two fragments of a tree.
It are necessary to say only, that that select () could create objects node it is necessary for him to specify dom the document. And to recreate a tree of objects, it is necessary to specify also object (reception node) in which this tree will be placed.
And now an example.
<? php
// We create the site to which the recreated tree will be added.
$resultnode = and $dom-> createelement (' resultnode ');
// We let know about it to object of a class myxtree.
$xtree-> setreceptionnode (and $resultnode);
$nodeset = $xtree-> select (" // node-1-1 // ");
pear:: iserror ($nodeset) and
trigger_error ($nodeset-> getmessage (), e_user_error);
As a result of performance of a method select () $resultnode site will contain node-1-1 site together with all descendants, and $nodeset will contain the list of links to node-1-1 site and all of his descendants. Depending on concrete needs{requirements} you can use this or that way of reception of result.
Updating of the data.
And here it is necessary to understand with a problem of comparison of two xml documents.
Why xml documents, instead of dom documents? Because data exchange occurs in xml a format. It is necessary for us to provide an opportunity to obtain the data sql a tree, to send for change to the user, and then received changes to bring back in sql in a tree. Departure given to the user and reception of them will occur back in xml a format (xforms on a threshold).
Give for some time we shall forget about sql a tree and we shall present, that we deal bo-«-ol`shim xml with the document.
It will be ours bo-«-ol`shoj And it is that part which was
The document it is changed by the user
<root>
<node-1-1 id = "1"> <node-1-1 id = "1">
<node-1-2> <node-1-2>
simple text unsimple text
</node-1-2> </node-1-2>
<node-2-2> </node-1-1>
<node-1-3>
very simple text
</node-1-3>
</node-2-2>
</node-1-1>
</root>
To make changes to us it is necessary to establish conformity between these to two xml documents. As xml the document this tree, elements are defined{determined} by the position in a tree. This way does not approach us. The name of an element too cannot serve as the identifier. It is possible to use attribute for this purpose. But text sites and cdata sites cannot have attributes. As to choose any more from what, has been solved to use attribute with a name "id" unique within the limits of all tree. And for identification text and cdata sites to use attribute "id" their parent. Consequence{investigation} of such decision became that elements should have only one child such as text_node or cdata_section_node. Besides if the site such as element_node has no attribute "id", attempt to identify it on attribute "id" the parent and named the element also will be executed. There is even a way of identification of an element on attribute "id" and by name an element. I.e. The attribute "id" in that case should be unique only within the limits of the specified name. The given way is not realized in myxtree.
So, we were defined{determined} WHAT to update. Now it is necessary to understand HOW to update. The most simple, that comes to mind it to replace one with another. I.e. (see the example is higher) to replace node-1-1 site in big xml the document on corresponding. We shall name this way full updating (at such way of updating we do not have need worries about conformity of affiliated elements, it is enough that only node-1-1 site had attribute "id"). However in big xml the document node-1-1 site has one more affiliated element node-2-2, at such way of updating he will be removed from a tree. When it is allowed to user to change structure of a tree, it and is necessary for us. However there are situations when it is necessary to update only a part of descendants of node-1-1 site. In that case we need to use selective updating. At such way those sites which have been really changed will be updated only. If new elements have appeared, they will be added. Such algorithm of updating not yet final variant. Basically he has developed in a course of the decision of a specific target. And here your opinions will be interesting.
Use of a method update () does not represent any complexities. The first parameter is a site which needs to be updated, the second parameter specifies that what to update it is necessary not only the specified element but also descendants, the third parameter defines{determines} a way of updating: true - full, false - selective.
<? php
$result = $xtree-> update (and $dom-> documentelement, $deep = true, $full = false);
pear:: iserror ($result) and
trigger_error ($result-> getmessage (), e_user_error);
If the algorithm of updating of you does not arrange, always there is an opportunity of "manual" serial updating. In that case the parameter $deep should be false.
It is a little about job of a class.
The parameter $recursive for a method select () was above mentioned. There has come time to tell about it more in detail.
Than are good sql trees with model of the enclosed sets? That, that for one sql search it is possible to receive the site with all his descendants irrespective of depth of an investment. Than plokhi sql trees? That what to make such sql search, to put it mildly, uneasy. During too time there is a good language of searches to treelike structures - xpath. So has been solved to try to use language xpath for sql trees. It has turned out. But what price?
At construction sql search on xpath to expression self-association of tables is used. For example, expression "/root" will create sql search of a kind:
select * from objects, objects as table1, objects as table2 where...
Self-association means, that the table will be multiplied by itself. I.e. the table of 100 lines will generate 10000 lines! Terrible figure! Such result put expediency of the project under doubt. Therefore has been realized also other way of processing xpath expressions - recursive. The name of the second way speaks for itself.
After the class has earned, competition between the first (selfjoin) and the second (recursive) in the way as a result of which self-association has been rehabilitated has been lead{has been carried out}! It is difficult to say in what way, but mysql with success copes with this huge problem . The opinion of experts on mysql here would be interesting.
Together with a class myxtree test files on which job of a class by two ways was compared are distributed also. In the test it is at first created dom the document from 1000 elements. Then this document is saved in sql a tree, and, at last, the method select () makes sample of elements by two ways. As searches expressions of a kind " / * ", "/*/* ", "/*/*/* ", etc. up to ten levels of an enclosure are used. Really dom the document has 5 levels of an enclosure. And in one case at each element is only on one affiliated element, and in the friend on 5. I.e. the quantity{amount} of chosen elements grows with each level. Both in that and in the other variant the way of self-association of tables surpassed a recursive way.
In a pursuit of speed.
Likely many will want to take an interest, how quickly all this works?
Question too ambiguous that on him it was possible to answer unequivocally. What criteria of a rating to use? Learn{find out} concrete execution time you can on the computer, having started tests applied to myxtree. It would be possible to compare speed of job myxtree to other class with similar functionality, but for today to me such are not known. There are interfaces to sql to trees, but without support dom and xpath. And it in turn affects speed (the more functionality, the speed there is less). There is and more one criterion of a rating - speed of development. And time spent for development, costs{stands} much more dearly{expensively}, than execution time of a script. How much you will spend time to make a script working half-seconds faster. Whether there is it of those efforts. myxtree will help to save time of development. It is necessary to understand once with it . How to be spoken " it is better than midday to study to fly, and then for five minutes to reach ", in fact skill to fly remain.

|