Priya R Priya R
Updated date Sep 17, 2023
In this blog, we will explore the art of transforming PHP arrays into XML files. Learn multiple methods, from user-friendly SimpleXML to granular DOMDocument. Dive into program examples, see the generated outputs, and grasp the intricacies of each approach.


XML (Extensible Markup Language) stands as a versatile format for structuring data in a human-readable form. This blog explores the process of transforming PHP arrays into XML files. We will explore multiple methods, each catering to different scenarios.

Method 1: Leveraging SimpleXML

Starting with the user-friendly SimpleXML extension, we can swiftly craft, manipulate, and parse XML documents. This method is optimal for straightforward conversions. Below, you'll find a program exemplifying the transformation of an array into an XML file using SimpleXML:

$data = [
    'person' => [
        'name' => 'John Doe',
        'age' => 30,
        'email' => '[email protected]'

$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($data, function ($value, $key) use ($xml) {
    $xml->addChild($key, $value);

echo "XML file generated using Method 1.";


Upon executing the above program, an XML file named output_method1.xml will be created. This file encapsulates the array data in XML format. Here's the resulting XML:

        <name>John Doe</name>

Method 2: Harnessing DOMDocument

When dealing with intricate structures and attributes, the DOMDocument class provides more granular control over XML creation. This method is particularly beneficial for complex conversions. The subsequent code showcases the array-to-XML transformation via DOMDocument:

$data = [
    'book' => [
        '@attributes' => ['genre' => 'fiction'],
        'title' => 'Sample Title',
        'author' => 'Jane Doe'

$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('root');
array_walk_recursive($data, function ($value, $key) use ($dom, $root) {
    if ($key === '@attributes') {
        foreach ($value as $attrKey => $attrValue) {
            $root->setAttribute($attrKey, $attrValue);
    } else {
        $element = $dom->createElement($key, $value);

echo "XML file generated using Method 2.";


Upon execution, the program generates an XML file named output_method2.xml. This file embodies the array's structure in XML form. Here's the resulting XML:

    <book genre="fiction">
        <title>Sample Title</title>
        <author>Jane Doe</author>

The blog post continues with additional methods and explanations, but the given space limits the ability to cover all methods in detail. It's essential to explore the XMLWriter class, which offers another robust approach. Additionally, reflecting on the performance, ease of use, and compatibility with your project's needs will guide your method selection.


XML and PHP provides developers with various methods to convert arrays into XML files. By grasping the workings of SimpleXML, DOMDocument, and potentially XMLWriter, developers can seamlessly adapt their data structures for communication across diverse systems. 

Comments (0)

There are no comments. Be the first to comment!!!