PHP and XML Tutorial

PHP and XML


Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we will be discussing about PHP and XML.

What is XML?

XML is a markup language that looks a lot like HTML. An XML document is a plain text and it holds tags delimited by <and>. There are two big differences between HTML and XML.

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

XML provides you a lot more freedom than HTML. HTML has a certain set of tags: the opening <a> and closing </a> tag surround a link, the <p> tag begins a paragraph, and so on. However, an XML document can use any tags you want. Place <rating> </rating> around a movie rating, <height> </height> around someone's height. Thus Xml gives you an option to form your own tags.


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

HTML list that is not a valid XML -

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

This is not a valid XML document because there are no closing </li> tags to match up with the three opening <li> tags. All opened 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

Parsing an XML Document

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

To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string(). It returns back 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 use the simplexml_load_file( filename ) function if you have xml content in a file.


Generating an XML Document

Generating an XML Document

SimpleXML is good in parsing existing XML documents, but it cannot be used to creat a new one from scratch.

The easiest method of generating an XML document is by building a PHP array whose structure reflect that of the XML document and then iterating through the array, printing each element with the proper 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 our next tutorial guide, we will be studying about the Object Oriented Programming in PHP.

Feel free to ask your questions where necessary and we 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.