| home | community | downloads | matrix | search | news | join | members: 4815 |
|
Sun, 2008-05-04 17:57
In the last few weeks while looking for a position as a PHP programmer I have received several standard replies. One of them was "Can you go to our web site and answer the questions on PHP 5 and XML? Though I never give replies like these any place in my mind. I did feel the necessity to look into the latest features of PHP5 SimpleXML. I saw that many things have changed since I tried handling XML in PHP. To be honest I found the PHP4 methods to be really sucky. I found myself using these huge classes with a method to cover each and every small change in the XML. SimpleXML looks to be a much better choice and seems to be getting better with each version. The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators. You can read, write or iterate over your XML file with ease accessing elements and attributes. Export and ImportThe opening of XML file is possible with the function simplexml_load_file (). It takes in parameter URI or URL and returns an object of the class simpleEMLElement. Example: $xml = simplexml_load_file(pathway or URL); It’s also possible to interpret a string of XML into an object with simplexml_load_string (). This function will take the well-formed xml string data and return an object of class SimpleXMLElement with properties containing the data held within the xml document. If any errors occur, it returns FALSE. Example: Export and save of XML codeIt’s possible to save a part or totality of a XML document with the asXML function. For that, the asXML method formats the parent object's data in XML, or of course a node of document XML. It returns the contents of a file XML in a character string or directly in a file.
<?phpExample2: To save in a file
<?phpHandling of elementsThe parsing of data in XML is also very simple. I'll make an example in order to illustrate the handling of elements. Example of file data in XML:
To access the node by nameThere are several cases to access to node. To access to only one node the following code is used:
<?phpThis script will display: "PHP:Behind the parser" “PHP solves all my web problems”. This is because although the code is valid it does not say which parent element to start with so XML and PHP default to the first in the list. Also take notice of the "great-lines" element. XML allows "-" within names but the PHP SimpleXML parser does not recognize it as valid without a little help. The string needs to be encapsulated within the code call.
<?phpIn this example XML we have a file containing several tags “album”, we can display the first tag album or to list all the albums. For that we use the following code:
<?phpThis example will display: PHP 5: A New World
To access given nodesTo find children of given node the function children is used (). I don't need to know the name of nodes. This method finds the children of the element of which it is a member. To display the single first element the XML tree:
<?phpTo display multiple children of an element:
<?phpHandling valuesOne of the nice things about how PHP does XML is that the values in a file do not have to be constant or permanent. You can change them and save those changes to a new file. I found this to be interesting having come from ASP and the use of Excel files as a data source. Manipulation of data in an excel file is a pain. But doing something similar in PHP5 and XML is cake.
<?phpHandling of the attributesTo access to an attribute, it is very simple. In our example we have tags albums with the attribute language, thus $xml [“rating”], with $xml the object representing our file XML, is an associative table of the attributes rating. To list all the attributes of a node it is necessary to use the function attributes (). This function returns us an associative table with all the attributes out of key and the value associated. Example 1:
<?phpUpdating, Adding and RemovingNow comes a good reason for keeping your PHP version updated to the latest one possible. Doing the following was not possible with PHP 5.13 or PHP 4 without getting into some heavy string manipulation code. Example2:
<?phpSo if anyone starts giving you a hard time about wanting the latest version of PHP tell them to stick it! At my present job one of the reasons that I am happily looking for different employment is because to their constant need to get things done quickly be refusal to at least try and use PHP5.
Tags: Developer Zone |
|
NewsletterGet updates on Hiveminds services, articles and downloads by signing up for the newsletter. |
Editor's choiceSome of the better articles, stories and tutorials found at Hiveminds. |
Find moreFind more of Hiveminds articles, stories, tutorials and user comments by searching. |
Picked linksHand picked websites and articles from around the web that provide quality reading. |