PHP and XML
So, we shall talk about XML. What is such and why many professional programmers prefer it to all other formats? And why so many laudatory responses on the part of those who has collided{faced} it ? And why, at last, so few negative responses from those who with it did not collide{face}:-)? XML as language (and it is language, only not programming, and marking) was generated rather recently - officially the first edition of his specification has been published in 1998. this format appeared so successful, that at once was came to a court yard, and his realizations have gone away practically under all programming languages (the truth, for the present as external modules or obrabotchikov) - from Delphi up to PHP. However the Russian-speaking documentation on it has not enough, and language is so scaled and flexible what to describe all areas of his application it is simply impossible. Therefore domestic programmers just start to comprehend secrets XML and while only try to put it into practice.
Abbreviation XML is deciphered and translated as « an expanded markup language ». And in it all his essence. Basically, the programmer himself defines{determines} a format of a file and itself writes it obrabotchik, using for this purpose means given by language or developing own. Now kilometers of an initial code are necessary for processing hundreds text files for nobody - all this is easily replaced with one XML-file and one parserom (obrabotchikom).
The XML-file is an ordinary text file which data are organized so that to create hierarchical structure (tree) of tags. The programmer thinks out names and attributes of tags independently, and rules of their spelling are similar to those in HTML. For example, for a bookshop:
<? xml version = " 1.0" encoding = "UTF-8"?>
<shop>
<book author = “ Donald Knuth “> Art of programming </book>
<book author = “ Vasily Golovachev “> Magacitly </book>
</shop>
As you can see, even the person abstract from programming can understand, about what there is a speech in this file. In such simple way, applying treelike structure of a data structure, developers reach{achieve} full branch of contents (XML) from design (HTML and other) in what the purpose of considered{examined} language of a marking will consist.
Now about the basic concepts of a XML-marking. The quantity{amount} of tags in a file is not limited, no less than quantity{amount} of their attributes. The XML-document should be made correctly, according to the following rules, many of which pereklikajutsja with specification HTML:
1. <? xml version = " 1.0"?> - the first line always contains the version of the specification, but can be and additional attributes - for example, the coding of symbols of the document.
2. At the document there is one and only one root element (a pair tag <shop> in our case), is similar <html> </html> in language HTML. All affiliated elements can contain any quantity{amount} of the enclosed tags which, in turn, too can contain any quantity{amount} of descendants at the expense of that it is provided drevovidnost`.
3. All without exception tags should have corresponding closing elements. If in HTML it was possible to lower{omit}, for example, some closing tags, and it was considered correct even according to the specification in XML it is invalid as will instantly call a mistake of processing. To tell the truth, if in a tag it is not planned to create any enclosed elements (whether it be contents or other tag) to close it are possible a little bit easier (for example, <song name = " Only you "/> instead of <song name = " Only you "> </song>)
4. All attributes of tags need to be quoted - double or unary.
5. All other rules to not block up clause{article}, read in the specification which is to the address http://www.w3.org/TR/REC-xml.
PHP and XML
So, with XML have more - less understood. Anyway, according to rules of a XML-marking you can already create the simple structured file, especially knowing bases HTML. Now let's start realization of functions of processing of XML-contents in language PHP.
I think, it is not necessary to be distributed especially about advantage{benefit} PHP as server programming language. All opportunities Perl plus « still something » - and we have the high-grade interpretive programming language which is carried out on the party of the server. Further it is supposed, that the server and PHP at you are already established and are properly adjusted to teamwork, and that you have the general{common} concepts about their functioning and programming (to beginning{starting} programmers we recommend to familiarize with a cycle of Artem SHmancyreva « the Server of a tribe of apaches », MK ¹¹ 38-40, 42, 44, 46, 50, 4, 9 (209-211, 213, 215, 217, 221, 227, 232) - primech. red.) Many hostings - providers are basically concerns to paid hostings - give PHP in which configuration modules of processing XML are already accessible. For a domestic platform I can recommend the following.
In last versions PHP (since version 4.3.0 for platform Windows as by the most widespread) it it is switched on the library php_domxml.dll which needs to be connected to the interpreter for reception of access to functions of processing XML (similar library is and in Linux, but there she is connected a little differently). This library is in the catalogue extension_dir, registered in a configuration file php.ini, which in turn lays (or should lay) in your folder Windows. In the same file raskommentirujte a line extension=php_domxml.dll, and you have an opportunity to operate with documents XML, applying objective model Document Object Model on which we today shall in detail stop. By the way, we shall consider{examine} opportunities PHP on job with XML-files on the elementary example - we shall write own guest book. The example, certainly, is idealized, but on servers with scripts I for the present did not meet the guest books written with application XML. Therefore, I hope, today's examples will be for you not only interesting, but also useful.
For the guest book we on the server shall create a file with a name guest.xml which will contain all left recordings in the following format:
<? xml version = " 1.0" encoding = "UTF-8"?>
<guestbook>
<message date = " 01.01.04" time = "11:22:25" author = "Cosmic" email = "cosmic@mail.zp.ua" subject = " New GB presented ">
Message body. No HTML tags here.
</message>
<message date = " 01.01.04" time = "12:22:25" author = "Cosmic" email = "cosmic@mail.zp.ua" subject = " This is a subject ">
Another message body
</message>
<message date = " 01.01.04" time = "10:22:25" author = "Cosmic" email = "cosmic@mail.zp.ua" subject = " i? ia? aiia ">
Message body. Attention! Subject in UTF-8 encoding!
</message>
</guestbook>
As follows from the first line, our XML-file will store{keep} the data in coding UTF-8. For what it is necessary, you learn{find out} hardly later. The root element <guestbook> contains set of affiliated elements <message> which are characterized by attributes date (date), time (time), author (author), email (the post address) and subject (subject). In a body of an element <message> there is actually a body of the message. As you can see, all is intuitively understandable and is a little bit similar to a database in which we name fields how that we want.
Before starting a spelling of the script, I want to say, that in PHP functions of job with XML while are realized experimentally (the truth, in version 5.0 on XML is made the special rate, only this version for the present is in a status bety, hence, to pass to it{her} for the present does not cost). It means, that the resulted scripts can not work at presence of other versions PHP which is not corresponding specified. Besides my scripts are far from perfect by way of productivity and beauty of a spelling. Therefore it is not necessary to fill up my poor mail box with angry letters. My business as the author, - natolknut` you on an idea, and realization of this idea remains exclusively for you.
PHP supports two modules which are carrying out XML-parsing. The first is called SAX (Simple API for XML). By virtue of complexity and the limited functionality (with help SAX it is impossible to write down the data in a XML-file - he supports only reading), this interface will not be considered{examined} by us today. The second module represents much more interest from the point of view of the developer as allows to work more evidently with a XML-file, and operations and readings, and recordings are supported. He is called DOM (Document Object Module) and is realized in PHP as follows.
With the help of function domxml_open_file, caused with a name of a XML-file as parameter (in our case - guest.xml), all tree of XML-elements from a file is loaded into memory of the computer - server (in a variable). It means, that any changes made with an open file, will not come into force before his compulsory recording on a disk. This technology is very convenient, as allows to recheck ten times a correctness of the actions made above a file before finally it to save. In general, in format XML the huge attention that should have an effect on his realizations in programming languages is given to a correctness.
In a XML-file as it has already been said, there can be only one root element. The root element can be received, using function document_element (), caused without parameters and returning object. All affiliated elements are stored{kept} in the received object (tags <message>) which we can receive, having called function child_nodes (), returning a file of affiliated tags. Now, projdjas` on the received file a cycle foreach, it is possible to receive attributes of each of affiliated elements (function get_attribute (), caused with a name of attribute as parameter) and contents of these elements (function get_content () without parameters). Each element, affiliated for root, can be root for the enclosed elements if those are available. In that case they can be received also in the enclosed cycle foreach, projdjas` him on an element of the top level.
For our guest book the script of reading of messages will look as follows:
// Opening a XML-file
$xml = domxml_open_file ('guest.xml');
// Reception of a root element
$root = $xml-> document_element ();
// Reception of a file of the enclosed tags (descendants)
$nodes = $root-> child_nodes ();
foreach ($nodes as $node) {
// If the name of the descendant corresponds{meets} necessary...
if ($node-> node_name () == 'message') {
//.. We fill the called file of the current message with the data from a file...
$currentMessage ['date'] = $node-> get_attribute ('date');
$currentMessage ['time'] = $node-> get_attribute ('time');
$currentMessage ['author'] = $node-> get_attribute ('author');
$currentMessage ['email'] = $node-> get_attribute ('email');
$currentMessage ['subject'] = $node-> get_attribute ('subject');
$currentMessage ['content'] = $node-> get_content ();
//.. Also we add the filled element in a file
$messages [] = $currentMessage;
}
}
After performance of a script we receive the filled file $messages, projdjas` on which in a cycle foreach, we without ceremony receive messages and we deduce{remove} them in a body of page:
foreach ($messages as $item) {
echo $item ['date'];
echo $item ['time'];
echo utf8_decode ($item ['author']);
echo $item ['email'];
echo utf8_decode ($item ['subject']);
echo utf8_decode ($item ['content']);
}
As a result of performance of a script on a page in one line all messages will be deduced{removed} the guest book. I intentionally do not do{make} any registration (for example, it would be possible to deduce{remove} all messages in the table, having divided{shared} all fields and having made the message legible) as clause{article} in this case will be stretched{will be dragged out} on ten numbers{rooms}. Once again I shall repeat, that my problem - natolknut` you on idea. The everything else - business of your personal taste and preferences. Guest you can see a working variant to the address http://www.cosmic.net.ua/gb.
As I already spoke, model DOM allows not only to read the data from a XML-file, but also to write down them, adding new elements or attributes. To write down the data, it is necessary to open again a XML-file with the help of function domxml_open_file and to receive a root element of a tree (function document_element ()). Further, with the help of function new_child () at the end of set of available affiliated tags we create a new affiliated element. Function accepts two parameters - the name of an element and actually his contents. Attributes of an affiliated tag <message> are established by means of function set_attribute (), as parameters accepting the name of attribute and his value.
As we write down messages of the guest book, we should receive them all over again from a file $ _GET or $ _POST to which they should be transferred{handed} from the form (if you do not know how it is done{made}, can read about it in my previous clauses{articles}). From the received data we prepare for the called file of attributes which in the future will be written down in a tag <message>:
$msgToAdd = array (
'date' => date ("d.m.y"),
'time' => date ("H:i:s"),
'author' => utf8_encode ($author),
'email' => $email,
'subject' => utf8_encode ($subject)
)
As you can see, before recording of variables $author and $subject (and in the future and bodies of the message), we shall recode them in coding UTF-8. It is done{made} for the reason, that at given stage PHP, unfortunately, does not support coding Windows-1251 in XML-files. It means, that at attempt will write down the message in Russian a script to give out a mistake. Therefore it is necessary to code symbols in the intermediate coding (function utf8_encode), and then at reading them to decode (function utf8_decode). All script of recording of the new message of signs about the following kind:
$xml = domxml_open_file ('guest.xml');
$root = $xml-> document_element ();
// $message - a body of the message from a file $ _GET or $ _POST
$msgNode = $root-> new_child ('message', utf8_encode ($message));
$msgNode-> set_attribute ('date', $msgToAdd ['date']);
$msgNode-> set_attribute ('time', $msgToAdd ['time']);
$msgNode-> set_attribute ('author', $msgToAdd ['author']);
$msgNode-> set_attribute ('email', $msgToAdd ['email']);
$msgNode-> set_attribute ('subject', $msgToAdd ['subject']);
$text = $xml-> dump_mem ();
$fp = fopen ('guest.xml', 'w');
fwrite ($fp, $text);
fclose ($fp);
Here there is a new function dump_mem (), throwing off all contents of memory in a variable $text which further enters the name on a disk standard operators of recording. As you can see, all is elementary simple, safe and correct - while you are not convinced, that all operators are executed correctly, your file will not be written down and, hence, reliability of a data storage increases and the probability of their loss decreases.
In summary we shall talk about possible{probable} scopes of the considered technologies. XML, despite of the universality, nevertheless is not panacea, therefore it is not necessary to throw and pass at once all to use of a new format, not having convinced in his absolute{hundred-percent} necessity. XML can help, for example, if it is beforehand not known, what client will process the data (whether it be an Internet - browser or completely independent client written by foreign developers). It is very convenient to do{make} an outcome between the data and registration, building patterns of the pages on samples of XML-file.s it is even more convenient to do{make} sites in several languages, using built - in means XML for the organization mul`tijazychnogo the interface.
So as always the choice remains for you, dear readers.
Success!

|