How to convert associative array to XML in PHP
In this article, you will learn how to convert an associative array to XML using the PHP programming language.
In applications, we generally use associative arrays as a data dictionary to hold data in key-value pairs. It structures a good mapping between a key and a value. So let's know how to convert an associative array to XML in PHP.
In web applications, XML (Extensible Markup Language) is used for many aspects. The data stored in XML can be rendered easily in many applications. That's why it is the best choice for web services and data transport. It stores data in a formatted way that is easy to search and understand. It makes it easier for an informative application to store, retrieve, and display data. It is widely used in e-commerce applications, business-to-business transactions, generating metadata, and so on.
Suppose we have the following associative array that contains student records.
<?php
$students = array(
array('name' => 'Priska',
'age' => 10,
'class' => '5A',
'gender' => 'F'),
array('name' => 'Abhi',
'age' => 12,
'class' => '6B',
'gender' => 'M'),
array('name' => 'Smith',
'age' => 11,
'class' => '5B',
'gender' => 'M')
);
?>
To generate XML, first we need to call header() method to set the header content-type for the document. For generating XML, the header content-type should be 'text/xml'.
header('Content-Type: text/xml');
Next, start the XML tag like this and loop over the associative array.
'<?xml version="1.0" encoding="UTF-8"?>'
Here is the complete code to convert an associative array to XML using PHP. There are several libraries and methods available to generate XML, but we are using simple PHP code to generate XML. In the foreach loop, each item is enclosed within an XML tag. We have also used the htmlspecialchars() method to convert some special characters (&, ", <, >) to HTML entities.
<?php
header('Content-Type: text/xml');
$xml ='<?xml version="1.0" encoding="UTF-8"?>';
$xml .='<school>';
$students = array(
array('name' => 'Priska',
'age' => 10,
'class' => '5A',
'gender' => 'F'),
array('name' => 'Abhi',
'age' => 12,
'class' => '6B',
'gender' => 'M'),
array('name' => 'Smith',
'age' => 11,
'class' => '5B',
'gender' => 'M')
);
foreach ($students as $student) {
$xml .='<students>';
foreach($student as $tag => $data) {
$xml .='<'.$tag.'>'.htmlspecialchars($data).'</'.$tag.'>';
}
$xml .='</students>';
}
$xml .='</school>';
echo $xml;
?>
When we call the above script in the browser, it returns the following output.
<?xml version="1.0" encoding="UTF-8"?>
<school>
<students>
<name>Priska</name>
<age>10</age>
<class>5A</class>
<gender>F</gender>
</students>
<students>
<name>Abhi</name>
<age>12</age>
<class>6B</class>
<gender>M</gender>
</students>
<students>
<name>Smith</name>
<age>11</age>
<class>5B</class>
<gender>M</gender>
</students>
</school>
Related Articles
PHP sanitize input for MySQLPHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
How to Pass an Array as URL Parameter in PHP
How to generate pdf in PHP using MySQL and MPDF Library
How to Export MySQL Table data as CSV file in PHP
Read CSV file & Import data into MySQL with PHP
How to generate PDF file using mPDF in PHP
Export data from MySQL table to CSV file using php
Simple XML Manipulation In PHP
How To Create Text Image Using PHP
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP