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

<?php
   $data = "<?xml version = '1.0' encoding = 'UTF-8'?>
   
   <note>
      <Course>Android</Course>
      <Subject>Android</Subject>
      <Company>cotocus</Company>
      <Price>$10</Price>
   </note>";
   
   $xml = simplexml_load_string($data) or die("Error: Cannot create object");
?>
<html>

   <head>
      <body>
         
         <?php
            print_r($xml);
         ?>
      
      </body>
   </head>
   
</html>

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"?>
<company>
 <employee>
 <firstname>Abhishek </firstname>
 <lastname>singh</lastname>
 <designation>php developers</designation>
 <salary>200000</salary>
 </employee>
 <employee>
 <firstname>Abhijeet </firstname>
 <lastname>singh</lastname>
 <designation>.net developers</designation>
 <salary>250000</salary>
 </employee>
</company>

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

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

Output:

Abhishek
Abhijeet

How to Create an XML document using PHP

<?Php
$xml =new DOMDocument ("1.8");
$root=$xml->createElement("data");
$xml->apppendChild($root);

$id=$xml->createElement("id");
$idText=$xml->createTextNode(","):
$id->appendchild($idText);

$title=$xml->createElement("title");
$titletext=$xml->createTextNode("Xml PHP Demo")
$title->appendChild($titleText);

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

$root = appendChild($book);

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

?>
Output:
<data>
<book>
<id>1</id>
<title>"XML PHP DEMO"</title>
</book>
</data>

XML Document Example

 <employee>
 <firstname>Abhishek </firstname>
 <lastname>singh</lastname>
 <designation>php developers</designation>
 <salary>200000</salary>
<sec>
 <firstname>Abhijeet </firstname>
 <lastname>singh</lastname>
 <designation>.net developers</designation>
 <salary>250000</salary>
 </employee>

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

Inheritance:

  • 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

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:

<?php

class base

{

function add($a,$b)

{

$res=$a*$b;

echo "Multiplication = ".$res;

}

}


class child extends base

{

function add($a,$b)

{

$res=$a+$b;

echo "Sum  = ".$res;

}

}

 $obj= new child();

 $obj->add(1000,500);
 
?>

Output:
Sum = 1500

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x