Tuesday, March 17, 2020

XML and PHP

What is XML?

  • XML stands for eXtensible Markup Language
  • XML is a markup language much like HTML
  • XML was designed to store and transport data
  • XML was designed to be self-descriptive
  • XML is a W3C Recommendation

The Difference Between XML and HTML 


XML is not a replacement for HTML. 

XML and HTML were designed with different goals: 
  • XML was designed to describe data, with focus on what data is 
  • HTML was designed to display data, with focus on how data looks 
HTML is about displaying information, while XML is about carrying information.

XML Does Not DO Anything 


Maybe it is a little hard to understand, but XML does not DO anything. 

The following example is a note to Tove, from Jani, stored as XML: 
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

The note above is quite self-descriptive. It has sender and receiver information, it also has a heading and a message body. 

But still, this XML document does not DO anything. It is just information wrapped in tags. Someone must write a piece of software to send, receive or display it.

With XML You Invent Your Own Tags


The tags in the example above (like <to> and  <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document. 

That is because the XML language has no predefined tags. 

The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like <p>, <h1>, etc.). 

XML allows the author to define his/her own tags and his/her own document structure. 

Features of XML

1. XML focuses on data rather than how it looks: One of the reasons, XML is popular because it focuses on data rather than data presentation. The other markup language such as HTML is used for data presentation. This separates the data and its presentation part and gives us the freedom to present the data, the way we want, once we receive it using XML.

Two or more systems can receive the same data from the same XML and present it in a different way using other markup languages such as HTML.

2. Easy and efficient data sharing: Since XML is software and hardware-independent, it is easier to share data between different systems with different hardware and software configurations. Any system with any programming language can read and process an XML document.

3. Compatibility with other markup language HTML: It is so much easier to read the data from XML and display it on a GUI(graphical user interface) using HTML markup language.

When the data changes over time, we need not make any changes in the HTML.

4. Supports platform transition: The main reason why changing to new systems and platforms is challenging because it involves the headache of data conversion between incompatible formats which often results in data loss. XML simplifies this process as the data is transported on new upgraded systems without any data loss.

5. Allows XML validation: A XML document can be validated using DTD or XML schema. This ensures that the XML document is syntactically correct and avoids any issues that may arise due to the incorrect XML.

6. Adapts technology advancements: The reason why XML is popular and being used for a very long time is that, it can adapt to the new technologies because of its platform-independent nature.

7. XML supports Unicode: XML supports Unicode that allows it to communicate almost any information in any written human language.

Parsing XML Document using DOM Parser

  • Very fast for small documents, load the entire document into memory.
  • Simple PHP DOM interface.
  • Access anything in a tree, traversed in a multitude of ways.
  • XML parsing library, libXML2 use for a DOM.
DOM Disadvantages
  • Load the entire document into memory.
  • Data access only after completely document parsed.
  • Support PHP 5+ or later version.

XML DOM tree for the books XML
                           Figure: XML DOM tree for the books XML

Create XML using DOM with Forms

Before starts to create an XML file using Html Form, we have to first create a file named studentdb.xml and create a starting tag as root tag.

<root> </root>

insertInXml.php: 
<?php  
 if(isset($_POST['submit']))  
 {  
   $name = $_POST['name'];  
   $add = $_POST['add'];  
   $xml = new DomDocument("1.0","UTF-8");  
   $xml->load('studentdb.xml');  
   $rootTag = $xml->documentElement;  
     $infoTag = $xml->createElement("info");  
     $nameTag = $xml->createElement("name",$name);  
     $addTag = $xml->createElement("address",$add);  
   $infoTag->appendChild($nameTag);    
   $infoTag->appendChild($addTag);    
   $rootTag->appendChild($infoTag);  
 $xml->save('studentdb.xml');  
 echo "Node Insereted";  
 }  
 ?>
 <html lang="en">  
 <head>  
   <meta charset="UTF-8">  
   <meta name="viewport" content="width=device-width, initial-scale=1.0">  
   <title>Document</title>  
 </head>  
 <body>  
 <h2>Student Info to be Inserted</h2>  
   <form method="POST">  
   Name <input type="text" name="name"><br><br>  
   Address <input type="text" name="add"><br><br>  
   <input type="submit" name="submit">  
   </form>  
 </body>  
 </html>
Result Output
studentdb.xml
<root>
<info>
<name>Pankaj Kapoor</name>
<address>Yamunanagar</address>
</info>
<info>
<name>Sachin Kumar</name>
<address>Vrindavan</address>
</info>
</root>

Manipulate XML using DOM

Manipulate the above-created XML file.
deleteInXml.php
<?php
 if(isset($_POST['submit']))  
 {  
   $name = $_POST['name'];  
 $xml = new DomDocument("1.0","UTF-8");  
 $xml->load('studentdb.xml');  
   $xpath = new DOMXPATH($xml);  
   foreach($xpath->query("/root/info[name = '$name']") as $node)  
   {  
     $node->parentNode->removechild($node);  
   }  
 $xml->formatoutput = true;    
 $xml->save('studentdb.xml');  
 echo "Node Deleted";  
 }?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
   <meta charset="UTF-8">  
   <meta name="viewport" content="width=device-width, initial-scale=1.0">  
   <title>Document</title>  
 </head>  
 <body>  
   <form method="POST">  
   <h2>Student Info to be Deleted</h2>  
   Name <input type="text" name="name"><br><br>  
   <input type="submit" name="submit">  
   </form>  
   </body>  
 </html>  
Result Output
studentdb.xml
<root>
<info>
<name>Pankaj Kapoor</name>
<address>Yamunanagar</address>
</info>
</root>

Read XML using DOM

readXml.php
<?php  
  $xml = new DOMDocument();  
  $xml->load('studentdb.xml');  
  $root = $xml->documentElement;  
  echo "<h2>This document stores Student Information</h2>";  
  $infoTag = $root->getElementsByTagName( 'info' );  
  foreach( $infoTag as $info){  
   foreach( $info->getElementsByTagName('name') as $name ){  
     echo '<b>Student:</b> ' . $name->nodeValue;  
   }  
   foreach( $info->getElementsByTagName('address') as $add ){  
     echo '<b> Address:</b> ' . $add->nodeValue. "<br>";  
   }  
  }?>
Result Output
readXml.php

This document stores Student Information

Student: Pankaj Kapoor Address: Yamunanagar Student: Sachin Kumar Address: Vrindavan Student: manish Address: kanpur