|
|
|
BROWSE
All Titles WordPress Web Services SOA BPEL Web Graphics & Video Web Development RAW Portugues, Espanol, Italiano, French PHP/MySQL Oracle Open Source Networking & Telephony Moodle Microsoft & .NET Linux Servers jQuery Joomla! JBoss Java e-Learning e-Commerce Dynamics Drupal CRM Cookbook Content Management Beginner Guides Architecture and Analysis AJAX Future Titles Recently Published Titles This article by Yuli Vasiliev examines the object-oriented approach for developing PHP/Oracle applications, as an efficient means to reduce the development time and complexity, and increase the maintainability and flexibility of your applications. In the following sections, you will learn how to create a simple PHP class to interact with Oracle and then how that class can be modified and reused in different scripts. See More |
Cooking XML with OOP
Formation of XMLLet us look at the structure of a common XML document in case you are totally new to XML. If you are already familiar with XML, which we greatly recommend for this article, then it is not a section for you. Let's look at the following example, which represents a set of emails: <?xml version="1.0" encoding="ISO-8859-1" ?> So you see that XML documents do have a small declaration at the top which details the character set of the document. This is useful if you are storing Unicode texts. In XML, you must close the tags as you start it. (XML is more strict than HTML, you must follow the conventions.) Let's look at another example where there are some special symbols in the data: <?xml version="1.0" encoding="ISO-8859-1" ?> This means you have to enclose all the strings containing special characters with CDATA. Again, each entity may have some attributes with it. For example consider the following XML where we describe the properties of a student: <student age= "17" class= "11" title= "Mr.">Ozniak</student> In the above example, there are three attributes to this student tag—age, class, and title. Using PHP we can easily manipulate them too. In the coming sections we will learn how to parse XML documents, or how to create XML documents on the fly. Introduction to SimpleXMLIn PHP4 there were two ways to parse XML documents, and these are also available in PHP5. One is parsing documents via SAX (which is a standard) and another one is DOM. But it takes quite a long time to parse XML documents using SAX and it also needs quite a long time for you to write the code. In PHP5 a new API has been introduced to easily parse XML documents. This was named SimpleXML API. Using SimpleXML API you can turn your XML documents into an array. Each node will be converted to an accessible form for easy parsing. Parsing DocumentsIn this section we will learn how to parse basic XML documents using SimpleXML. Let's take a breath and start. $str = <<< END The output is like this: SimpleXMLElement Object So now you can ask how to access each of these properties individually. You can access each of them like an object. For example, $sxml->email[0] returns the first email object. To access the from element under this email, you can use the following code like: echo $sxml->email[0]->from So, each object, unless available more than once, can be accessed just by its name. Otherwise you have to access them like a collection. For example, if you have multiple elements, you can access each of them using a foreach loop: foreach ($sxml->email as $email) Accessing AttributesAs we saw in the previous example, XML nodes may have attributes. Remember the example document with class, age, and title? Now you can easily access these attributes using SimpleXML API. Let's see the following example: <? This will display the text mime in the output window. So if you look carefully, you will understand that each node is accessible like properties of an object, and all attributes are accessed like keys of an array. SimpleXML makes XML parsing really fun.
Parsing Flickr Feeds using SimpleXMLHow about adding some milk and sugar to your coffee? So far we have learned what SimpleXML API is and how to make use of it. It would be much better if we could see a practical example. In this example we will parse the Flickr feeds and display the pictures. Sounds cool? Let's do it. If you are interested what the Flickr public photo feed looks like, here is the content. The feed data is collected from http://www.flickr.com/services/feeds/photos_public.gne: <?xml version="1.0" encoding="utf-8" standalone="yes"?> Now we will extract the description from each entry and display it. Let's have some fun: <? This will create the following output. See, how easy SimpleXML is? The output of the above script is shown below:
Managing CDATA Sections using SimpleXMLAs we said before, some symbols can't appear directly as a value of any node unless you enclose them using CDATA tag. For example, take a look at following example: <? This will generate the following error: <br /> To avoid this problem we have to enclose using a CDATA tag. Let's rewrite it like this: <data> Now it will work perfectly. And you don't have to do any extra work for managing this CDATA section. <? However, prior to PHP5.1, you had to load this section as shown below: $s = simplexml_load_string($str,null,LIBXML_NOCDATA);
XPathAnother nice addition in SimpleXML is that you can query using XPath. So what is XPath? It's an expression language that helps you to locate specific nodes using formatted input. In this section we will learn how to locate a specific part of our XML documents using SimpleXML and Xpath. Let's have a look at the following XML: <?xml version="1.0" encoding="utf-8"?> This document simply states the workflow of an analysis task and then tells it what to do at which state. So now you want to search what to do when the task type is analysis and assigned to cto and current state is new. SimpleXML makes it really easy. Let's take a look at the following code: <? This will echo the following: clarify However there is something to remember while writing XPath. When your XPath is followed by / then it means that you should keep the exact sequence of your XML document. For example: echo count($s->xpath("//state"));This will output 2. //state means take the state node from anywhere in the document. Now if you specify task//state, it will return all states from under all tasks. For example the following code will output 3 and 3: echo count($s->xpath("//notify"));Now what if you want to find notify just under state, following assigned, following action? Your XPath query should be //state/assigned/action/notify. But if you want that, it should be exactly under the task node which is just under the root node, it should be /task/state/assigned/action/notify. If you need to match any attribute then match it as [@AttributeName1='value'] [@AttributeName2='value']. If you see the following XPath, it will be clear to you: //task[@type='analysis']/state[@name='new']/assigned[@to='cto'] DOM APISimpleXML in PHP is used to parse the document however it cannot create any XML document. For creating XML documents on the fly you have to use DOM API that comes bundled with PHP 5. Using DOM API you can also create page-scrapping tools fairly easily. In this section we will learn how to create XML documents using DOM API, and then we will learn how to parse existing documents and modify them. In the following example we will create just a basic HTML file: <? This will produce the following code: <html> That's fairly easy, right? Let's do some more: <? This will produce the following code. <html><body> So you can save this XML generated by the DOM engine using the following code entered into a file in your file system: file_put_contents("c:/abc.xml", $doc->saveHTML());Modifying Existing DocumentsDOM API helps to create XML document easily as well as provide easy access to load and modify existing documents. With the following XML we will load the file we just created a few minutes ago and then we will change the header test of the first h1 object: <?php The output is shown below: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" Other Useful FunctionsThere are some other useful functions in the DOM library. We are not going to discuss them in depth, however they are included in this section for a one line overview.
SummaryXML API in PHP5 plays a very important role in web application development, most notably the new SimpleXML API, which simplifies parsing with ease. Today XML is one of the most used data formats for almost all big applications. Therefore getting familiar with XML APIs and relevant technologies will definitely help you to design robust XML‑based applications more easily.
About the AuthorHasin Hayder is a Zend Certified Engineer and open-source enthusiast from Bangladesh. Besides his regular job as Technical Director at Trippert Labs (www.trippert.com), he is often found developing localized Bangla applications and blogging at http://hasin.wordpress.com. He lives in Bangladesh with his wife Ayesha, son Afif and plenty of toys around! Books from Packt Look for more books on PHP/MySQL |
TOP TITLES ![]()
|
| ||||||||