A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::open($save_path, $name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 132

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 290

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 164

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 233

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 313

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 354

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 296

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Session cannot be started after headers have already been sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once


Deprecated: Optional parameter $cat_url declared before required parameter $rowperpage is implicitly treated as a required parameter in /home2/umernaze/public_html/coderanks/application/models/client/ArticleModel.php on line 39

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.