What is XML And How To Use XML in PHP

A markup language is used to annotate text or add additional information.

It is a case sensitive. These are used everywhere in commerce to collaborate, coordinate and exchange data with other commerce. XML-coded data can go directly from a web page to a database without the need for middleware. It uses tag to define data present in a web page.

Tags are useful in providing specific meaning of the data. It stores the data and specifies it. it will never change HTML in the future, but it can produce new possibilities by creating modern features of HTML. 

Syntax:

  1. <root>  
  2.    <child>  
  3.       <subchild>  
  4.       </subchild>  
  5.    </child>  
  6. </root> 

Example: 

  1. <?xml version="1.0"?>  
  2. <Message>  
  3.     <to>Akhilesh</to>  
  4.     <from>Bhushan sir</from>  
  5.     <heading>Reminder</heading>  
  6.   <body>Please call me on urgent basis!</body>  
  7. </ Message >  

Characteristics of XML are as given below:

  • XML has structured format.
  • XML is strongly-typed format.
  • XML is validated.
  • XML is the relationship between data and it can simply describe hierarchical data.

Uses of XML

  • XML is used to settle the data and store.
  • It can simply be merged with style sheets to create required output.
  • It minifies the creation of a HTML document.
  • Use it for reloading and unloading a database.
  • XML can manifest any type of XML document.
How we can use XML in PHP
 
Step 1: create form and save file name xml.php save it. 
  1. <form action="" method="POST" >  
  2.   First name:<br>  
  3.   <input type="text" name="firstname" value=""><br>  
  4.   EMail Id:<br>  
  5.   <input type="text" name="EMail" value=""><br><br>  
  6.     
  7.   Phone:<br>  
  8.   <input type="text" name="Phone" value=""><br><br>  
  9.     
  10.   <input type="submit" value="Submit" name="submit_btn">  
  11. </form>   
Step 2: write a php code on top of xml file save it, 
  1. <?php  
  2. $xmldoc = new DomDocument( '1.0' );  
  3. $xmldoc->preserveWhiteSpace = false;  
  4. $xmldoc->formatOutput = true;  
  5.   
  6.   
  7. if(isset($_POST["submit_btn"]) && $_POST["submit_btn"]=='Submit'){  
  8.   
  9.     extract($_POST);  
  10.       
  11.     $productNum = $firstname;  
  12.     $name = $EMail;  
  13.     $category = $Phone;  
  14.   
  15.   
  16.     if(!file_exists('Contact.xml')){  
  17.           
  18.         $xml="<Contact>\n\t\t";         
  19.         $xml .="<User>\n\t\t";  
  20.         $xml .= "<Name>$productNum</Name>\n\t\t";  
  21.         $xml .= "<EMail>$name</EMail>\n\t\t";  
  22.         $xml .= "<Phone>$category</Phone>\n\t\t";      
  23.         $xml.="</User>\n\t";  
  24.         $xml.="</Contact>\n\r";  
  25.         $xmlobj=new SimpleXMLElement($xml);  
  26.         $xmlobj->asXML("Contact.xml");  
  27.                           
  28.     }else{  
  29.   
  30.   
  31.         if$xml = file_get_contents'Contact.xml') ) {  
  32.               
  33.           
  34.             $xmldoc->loadXML( $xml, LIBXML_NOBLANKS );  
  35.             // find the headercontent tag  
  36.             $root = $xmldoc->getElementsByTagName('Contact')->item(0);  
  37.             // create the <Contact> tag  
  38.             $product = $xmldoc->createElement('User');  
  39.              
  40.               
  41.             // add the product tag before the first element in the <headercontent> tag  
  42.             $root->insertBefore( $product$root->firstChild );  
  43.   
  44.             // create other elements and add it to the <product> tag.  
  45.             $nameElement = $xmldoc->createElement('Name');  
  46.             $product->appendChild($nameElement);  
  47.             $nameText = $xmldoc->createTextNode($productNum);  
  48.             $nameElement->appendChild($nameText);  
  49.   
  50.             $categoryElement = $xmldoc->createElement('EMail');  
  51.             $product->appendChild($categoryElement);  
  52.             $categoryText = $xmldoc->createTextNode($name);  
  53.             $categoryElement->appendChild($categoryText);  
  54.               
  55.               
  56.             $categoryElement = $xmldoc->createElement('Phone');  
  57.             $product->appendChild($categoryElement);  
  58.             $categoryText = $xmldoc->createTextNode($category);  
  59.             $categoryElement->appendChild($categoryText);  
  60.   
  61.               
  62.             $xmldoc->save('Contact.xml');  
  63.         }  
  64.   
  65.     }  
  66.   
  67. }  
  68. ?>  
Step 3: Execute xml.php file into xampp server like,

 
 
step 4:  When you execute xml.php file then Contract.xml automatic generate into your project root folder  
 
first screen execute file 
 
 
 
 
 
After fill information into form,
 
 
And click submit button .
 
output: To generate  Contact.xml file into your root folder, 
 
 
and open file into notpat++ show my information into xml file,

 
if you have fill another information at form then your information append on Contact.xml file,

Output: append another information 
 

Read more articles on XML:


Similar Articles