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:

Uses of XML

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. Phone:<br>
  7. <input type="text" name="Phone" value=""><br><br>
  8. <input type="submit" value="Submit" name="submit_btn">
  9. </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. if(isset($_POST["submit_btn"]) && $_POST["submit_btn"]=='Submit'){
  6. extract($_POST);
  7. $productNum = $firstname;
  8. $name = $EMail;
  9. $category = $Phone;
  10. if(!file_exists('Contact.xml')){
  11. $xml="<Contact>\n\t\t";
  12. $xml .="<User>\n\t\t";
  13. $xml .= "<Name>$productNum</Name>\n\t\t";
  14. $xml .= "<EMail>$name</EMail>\n\t\t";
  15. $xml .= "<Phone>$category</Phone>\n\t\t";
  16. $xml.="</User>\n\t";
  17. $xml.="</Contact>\n\r";
  18. $xmlobj=new SimpleXMLElement($xml);
  19. $xmlobj->asXML("Contact.xml");
  20. }else{
  21. if( $xml = file_get_contents( 'Contact.xml') ) {
  22. $xmldoc->loadXML( $xml, LIBXML_NOBLANKS );
  23. // find the headercontent tag
  24. $root = $xmldoc->getElementsByTagName('Contact')->item(0);
  25. // create the <Contact> tag
  26. $product = $xmldoc->createElement('User');
  27. // add the product tag before the first element in the <headercontent> tag
  28. $root->insertBefore( $product, $root->firstChild );
  29. // create other elements and add it to the <product> tag.
  30. $nameElement = $xmldoc->createElement('Name');
  31. $product->appendChild($nameElement);
  32. $nameText = $xmldoc->createTextNode($productNum);
  33. $nameElement->appendChild($nameText);
  34. $categoryElement = $xmldoc->createElement('EMail');
  35. $product->appendChild($categoryElement);
  36. $categoryText = $xmldoc->createTextNode($name);
  37. $categoryElement->appendChild($categoryText);
  38. $categoryElement = $xmldoc->createElement('Phone');
  39. $product->appendChild($categoryElement);
  40. $categoryText = $xmldoc->createTextNode($category);
  41. $categoryElement->appendChild($categoryText);
  42. $xmldoc->save('Contact.xml');
  43. }
  44. }
  45. }
  46. ?>
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: