Friday, October 9, 2020

PHP and XML Tutorial



Hello dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about PHP and XML.

What is XML?

XML can simply be described as a markup language that looks a lot like HTML. An XML document is a plain text and it contains tags that are delimited by <and>. There are two major differences between an HTML and XML.

  • Xml doesn't define a specific set of tags you must use.
  • Xml is exceedingly selective about data structure.

XML provides a lot more freedom than HTML. HTML has a certain set of tags: the opening <a> and closing </a> tag surrounds a link, the <p> tag starts a paragraph, etc. However, an XML document can make use of any tags you need. Put the <rating> </rating> around a movie rating, <height> </height> around a person's height. This Xml gives you an option to form your own tags.

RECOMMENDED POST: How to use AJAX in PHP

XML is so strict when it comes to document structure. HTML lets you play fast and loose with some opening and closing tags. But it is not the case with XML.

HTML list that's not a valid XML -

<ul>
   <li>Braised Sea Cucumber
   <li>Baked Giblets with Salt
   <li>Abalone with Marrow and Duck Feet
</ul>

The above list is not a valid XML document because there are no closing </li> tags to match up with the three opening <li> tags. Every opening tag in an XML document must also have a corresponding closing tag.

HTML list that is a valid XML -

<ul>
   <li>Braised Sea Cucumber</li>
   <li>Baked Giblets with Salt</li>
   <li>Abalone with Marrow and Duck Feet</li>
</ul>



Parsing an XML Document

PHP's SimpleXML module makes parsing XML document very easy. It turns an XLM document into an object that provides a structured access to the XML.

To create a simple XML object from an XML document stored in a string, then you have to pass the string to simplexml_load_string(). This returns a simple XML object.

Example

Try the following example below -

<html>
   <body>
      
      <?php
         $note=<<<XML
         
         <note>
            <to>Justice Amadi</to>
            <from>Web Design Tutorialz</from>
            <heading>Job Offer</heading>
            <body>You have been offered a job </body>
         </note>
         
         XML;
         $xml=simplexml_load_string($note);
         print_r($xml);
      ?>
		
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

SimpleXMLElement Object ( [to] => Justice Amadi [from] => Web Design Tutorialz [heading] 
=> Job Offer [body] => You have been offered a job )

Note - You can make use of the simplexml_load_file( filename ) function if you have xml content in a file.

RECOMMENDED: PHP MySQL Database


Generating an XML Document
SimpleXML is good for parsing existing XML documents, but it can not be used to creat a new one from scratch.

The easiest way to generate an XML document is to build a Php array whose structure reflects that of the XML document and then to iterate through the array, printing each element with the appropriate formating.

Example
Try the following example below -

  <?php
   $channel = array('title' => "What's For Dinner",
      'link' => 'http://menu.example.com/',
      'description' => 'Choose what to eat tonight.');
   
   print "<channel>\n";
   
   foreach ($channel as $element => $content) {
      print " <$element>";
      print htmlentities($content);
      print "</$element>\n";
   }
   
   print "</channel>";
?>

Output
When the above code is executed, it will produce the following result -

http://menu.example.com/ Choose what to eat tonight.


Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial guide, we will be discussing about Object Oriented Programming in PHP.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.