How to Create XML From PHP – Tutorial

arp14 PHP, Tutorial, XML Tags: , , , 0

In this tutorial we will learn, How to Create XML From PHP. For this tutorial, you must have basic knowledge of PHP and XML. If you are new to PHP and/or XML, I recommend you to see the tutorials on PHP and XML from links below.

So, lets start creating XML from PHP…

How to Create XML From PHP - Tutorial

The Code to Create XML from PHP

    <title>Create XML from PHP -</title>
    <h1>Create XML from PHP -</h1>
$data = array();
$data[] = array('brand' => 'Aston Martin', 'model' => 'DB9', 'year' => '2013', 'description' => 'This is a awesome car!');
$data[] = array('brand' => 'Lamborghini', 'model' => 'Reventon', 'year' => '2013', 'description' => 'This is a supercool car!');

for ($i = 0; $i < sizeof($data); $i++) {
    # write data to xml start ################
    $doc = new DomDocument( '1.0' );
    $doc->preserveWhiteSpace = false;
    $doc->formatOutput = true;

    if( $xml = file_get_contents( 'cars_db.xml') ) {
        $doc->loadXML( $xml, LIBXML_NOBLANKS );

        // find the <cars> tag
        $cars = $doc->getElementsByTagName('cars')->item(0);

        // create each <car> tag
        $car = $doc->createElement('car');
            // create other elements and add it to the <car> tag.
            $brand         = $doc->createElement('brand');
            $brand_val     = $doc->createTextNode( $data[$i]['brand'] );
            $brand->appendChild( $brand_val );

            $model         = $doc->createElement('model');
            $model_val     = $doc->createTextNode( $data[$i]['model'] );
            $model->appendChild( $model_val );

            $year         = $doc->createElement('year');
            $year_val     = $doc->createTextNode( $data[$i]['year'] );
            $year->appendChild( $year_val );

            $description         = $doc->createElement('description');
            $description_val     = $doc->createTextNode( $data[$i]['description'] );
            $description->appendChild( $description_val );

        if ($doc->save('cars_db.xml')) echo '<h1>XML from PHP saved successfully</h1>';
        else echo '<h1>Failed to write XML from PHP</h1>';
# write data to xml end ################

How this works (explanation)

In this example, we are creating a simple cars database in XML from PHP. At first, we initialize a blank data array (line 9). Data array is the main level 1 data container. Then we add level 2 data array to it using square brackets (line 10, 11). So, $data is now a 2d array where level 1 contains elements to add and level 2 contains data of each element to add.

Now, create a xml file with name cars_db.xml or whatever you want. Open up that file and add the code as below.

<?xml version="1.0"?>


Then we loop through data array using for loop (line 13). In the loop, we created a new DOM Document object $doc. For nice clean structured XML, we used

$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;

After that, we get existing contents of the XML file. In this example, we won’t overwrite the XML file – but we append new content to the file. We load the XML using loadXML method (line 20). Then we find the level 1 cars tag from the XML using getElementsByTagName method (line 23).

In the rest of the code, we create text node and element (tag) and then append the text node into that element.

  • To create a text node, use createTextNode method.
  • To create am element, use createElement method.
  • To append a text node or an element into another element, use appendChild method.


  1. Find the cars tag.
  2. One car tag for each loop created.
  3. Each specification tag for a car created.
  4. Each text node for each specification tag created.
  5. Text nodes appended to the correspondence specification tags.
  6. Each car tag is appended to the cars tag for each loop.
  7. File is saved.

Download Example Files to Create XML from PHP

Download XML from

Hope you understood how to create XML from PHP very well. If you still have problem, please comment below – I’ll be back to you.

Thank you!

Leave a Reply

Note: Your email address will not be published. Required fields are marked *