Logo
Code Ranks ×

XML handling in PHP

30/05/2019  .   3 minutes, 47 seconds to read  .   Posted by Admin
#php #XMLHandling

In this article, we'll learn the following points.

  1. XML Basics

  2. Reading XML document using PHP

  3. Creating an XML document using PHP

XML Document Basics

XML stands for Extensible Markup Language. XML is similar to HTML but in XML, we can create our own tags. It's used to store different data in the form of tags and attributes. It's used in SOAP APIs for data transfer b/w different applications.

Following is a basic XML document of team members.

<?xml version="1.0" encoding="utf-8"?>
<team>
    <member>
        <name>John</name>
        <skils>PHP, Codeigniter, Laravel, Slim, CakePHP, f3</skils>
        <occupation>PHP Developer</occupation>
    </member>
    <member>
        <name>Ben</name>
        <skils>Javascript, Jquery, Nodejs, Angular, Vue.js, React</skils>
        <occupation>Javascript Developer</occupation>
    </member>
    <member>
        <name>Thoumas</name>
        <skils>ASP.NET, WEB API, Winforms, ASP.NET MVC, Entity Framework, Linq</skils>
        <occupation>.NET Developer</occupation>
    </member>
    <member>
        <name>Michael</name>
        <skils>SQL Server, MySql, Oracle, MS Access, MongoDB</skils>
        <occupation>Database Developer</occupation>
    </member>
</team>

Reading XML document using PHP

To an XML document, we can use the simplexml_load_file function that will take document path as an argument and returns an object containing the data of the document. The following script will read the team.xml and display the details of every member.

<?php
$team = simplexml_load_file("team.xml");
$members = $team->member;

foreach ($members as $key => $member) {
    echo "Name: ".$member->name."<br>";
    echo "Skils: ".$member->skils."<br>";
    echo "Occupation: ".$member->occupation."<br>";
    echo "<hr/>";    
}

Here is how the output will look like.

Reading XML

Creating an XML document using PHP

We can use DOMDocument class to create an XML document. The following code will create an XML document and write all team members detail into it.

<?php
$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$xml_file_name = 'team_dynamic.xml';
$root = $dom->createElement('team');

//Creating member tag
$member_node = $dom->createElement('member');
//Creating name tag
$child_node_name = $dom->createElement('name', 'John');
$member_node->appendChild($child_node_name);
//Creating skils tag
$child_node_skils = $dom->createElement('skils', "PHP, Codeigniter, Laravel, Slim, CakePHP, f3");
$member_node->appendChild($child_node_skils);
//Creating occupation tag
$child_node_occupation = $dom->createElement('occupation', 'PHP Developer');
$member_node->appendChild($child_node_occupation);
//Adding member into team
$root->appendChild($member_node);

//Creating member tag
$member_node = $dom->createElement('member');
//Creating name tag
$child_node_name = $dom->createElement('name', 'Ben');
$member_node->appendChild($child_node_name);
//Creating skils tag
$child_node_skils = $dom->createElement('skils', "Javascript, Jquery, Nodejs, Angular, Vue.js, React");
$member_node->appendChild($child_node_skils);
//Creating occupation tag
$child_node_occupation = $dom->createElement('occupation', 'Javascript Developer');
$member_node->appendChild($child_node_occupation);
//Adding member into team
$root->appendChild($member_node);

//Creating member tag
$member_node = $dom->createElement('member');
//Creating name tag
$child_node_name = $dom->createElement('name', 'Thomas');
$member_node->appendChild($child_node_name);
//Creating skils tag
$child_node_skils = $dom->createElement('skils', "ASP.NET, WEB API, Winforms, ASP.NET MVC, Entity Framework, Linq");
$member_node->appendChild($child_node_skils);
//Creating occupation tag
$child_node_occupation = $dom->createElement('occupation', '.NET Developer');
$member_node->appendChild($child_node_occupation);
//Adding member into team
$root->appendChild($member_node);

//Creating member tag
$member_node = $dom->createElement('member');
//Creating name tag
$child_node_name = $dom->createElement('name', 'Michael');
$member_node->appendChild($child_node_name);
//Creating skils tag
$child_node_skils = $dom->createElement('skils', "SQL Server, MySql, Oracle, MS Access, MongoDB");
$member_node->appendChild($child_node_skils);
//Creating occupation tag
$child_node_occupation = $dom->createElement('occupation', 'Database Developer');
$member_node->appendChild($child_node_occupation);
//Adding member into team
$root->appendChild($member_node);

//Adding team into document
$dom->appendChild($root);
$dom->save($xml_file_name);
echo "$xml_file_name has been created";
?>

This code will create the same team XML document that we've discussed at the start of the article.