PHP XML Tutorial: Create, Parse, Read with Example What is XML?

XML parser

An XML parser  is a software library or package of  that provided interfaces for client application to work with an XML document. The XML parser is designed to read the XML and create a way for programs to use XML.

XML parser validates the document and check that the document is well formatted. Let’s understand   the working of XML parser by the figure below

   $data = "<?xml version = '1.0' encoding = 'UTF-8'?>
   $xml = simplexml_load_string($data) or die("Error: Cannot create object");


In PHP there are two major types of XML parsers:

  • Tree-Based Parsers
  • Event-Based Parsers

Tree-based parsers holds the entire document in Memory and transforms the XML document into a Tree structure. It analyzes the whole document, and provides access to the Tree elements (DOM).

DOM (Document object Model)

A DOM document is an object which contains all the document of an XML document is composed like a tree structure. The DOM parser implement a DOM API is very simple to use.

Feature of DOM parser

DOM parser create an internal structure in memory which is a DOM document object and client applications get information of the original XML document by invoking method on the document object DOM parser has a tree based  

Event-Based Parsers

Event-based parsers do not hold the entire document in Memory, instead, they read in one node at a time and allow you to interact with in real time. Once you move onto the next node, the old one is thrown away.

Example of event-based parsers:

  • XMLReader
  • XML Expat Parser

Read XML Elements in php

<?xml version="1.0"?>
 <firstname>Abhishek </firstname>
 <designation>php developers</designation>
 <firstname>Abhijeet </firstname>
 <designation>.net developers</designation>

In your PHP program, use the simplexml_load_file function to load an external XML file and create an object. After that, you can access any element from XML

  $xmldata = simplexml_load_file("employees.xml") or die("Failed to load");
  echo $data->employee[0]->firstname . "<\n>";
  echo $data->employee[1]->firstname;



How to Create an XML document using PHP

$xml =new DOMDocument ("1.8");


$titletext=$xml->createTextNode("Xml PHP Demo")

$book= $ml->createElement("book");
$book= $appendChild(id);
$book = appendChild($title); 

$root = appendChild($book);

echo <xmp>.$xml->saveXML()."</xmp>";
$xml->save('notes.xml') or die ('error');

<title>"XML PHP DEMO"</title>

XML Document Example

 <firstname>Abhishek </firstname>
 <designation>php developers</designation>
 <firstname>Abhijeet </firstname>
 <designation>.net developers</designation>

Why use XML?

  • NO page Reloading on each request
  • updated a part page not full page
  • send Data to server in background
  • Receive Data from server in background
  • Better user experience
  • Improves speed and performance


  • Accessing the features of one class from another class.
  • Inheritance allows you to write the code in the parent class and use it in both parent and child classes.
  • The parent class is also called a base class or super class. And the child class is also known as a derived class or a subclass.

Declaration of super Class :

class SuperClassName extends DerivedClassName
members of Super class

Type of Inheritance in php:

  • Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance


Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance

Real life example of polymorphism: A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism.

Example of Polymorphism:


class base


function add($a,$b)



echo "Multiplication = ".$res;



class child extends base


function add($a,$b)



echo "Sum  = ".$res;



 $obj= new child();


Sum = 1500

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x