Writing To XML With PHP OOP


simplexml_load_file() , simplexml_load_string() and SimpleXMLElement()

Both simplexml_load_file(), simplexml_load_string() and SimpleXMLElement() will allow you to add nodes to an XML file.

One of the main difference between the three are that you need to use the new keyword to instantiate a SimpleXMLElement() object. On the other hand, the simplexml_load_file() and simplexml_load_string() convert the xml to a SimpleXMLElement object.

Another difference between the three is that simplexml_load_file() loads a file while the other two load a string.

The examples below show how each of them can be used to append elements. One example will add the child node and display it in the browser while the other will add the text to the desired xml file. A node will only be added to a file once, regardless of how many times you hit the page.

simplexml_load_file()

Example #1

The below will load an xml file, append another member to the root node and print it in the browser.

<?php

$filename = 'filename.xml';

$doc = simplexml_load_file($filename); // load a file

$adding = $doc->addChild('member'); // main multiple elements with nested info
$adding->addChild('id', '1234');
$adding->addChild('name', 'Name Here');
$adding->addChild('phone', 'Phone Here');
$adding->addChild('email', 'Email Here');

header('Content-Type: text/xml');
echo $doc->asXML();

Example #2

The next example will append a new member to filename.xml.

$filename = 'filename.xml';

$doc = simplexml_load_file($filename); // load a file

$adding = $doc->addChild('member'); // main multiple elements with nested info
$adding->addChild('id', '1234');
$adding->addChild('name', 'Name Here');
$adding->addChild('phone', 'Phone Here');
$adding->addChild('email', 'Email Here');

echo $doc->asXML('test.xml');

simplexml_load_string()

Example #1

This example is just like above, except that the source is a string rather than a file. The PHP functions file_get_contents() and curl() can output a file to a string.

<?php

$filename = 'filename.xml';

$filename = file_get_contents($filename);
$doc = simplexml_load_string($filename); // load a string

$adding = $doc->addChild('member'); // main multiple elements with nested info
$adding->addChild('id', '1234');
$adding->addChild('name', 'Name Here');
$adding->addChild('phone', 'Phone Here');
$adding->addChild('email', 'Email Here');

header('Content-Type: text/xml');
echo $doc->asXML();

Example #2

This little snippet get the contents of the filename.xml file and return a string. After that, a new member is added to it.

$filename = 'filename.xml';

$filename = file_get_contents($filename);
$doc = simplexml_load_string($filename); // load a string

$adding = $doc->addChild('member'); // main multiple elements with nested info
$adding->addChild('id', '1234');
$adding->addChild('name', 'Name Here');
$adding->addChild('phone', 'Phone Here');
$adding->addChild('email', 'Email Here');

echo $doc->asXML('test.xml');

SimpleXMLElement

Example #1

This case will load a string and output the existing and new elements in the browser.

$filename = 'filename.xml';

/* PAIR */
$filename = file_get_contents($filename); // make the file contents a string
$doc = new SimpleXMLElement($filename); // needs a string as parameter

$adding = $doc->addChild('member'); // main multiple elements with nested info
$adding->addChild('id', '1234');
$adding->addChild('name', 'Name Here');
$adding->addChild('phone', 'Phone Here');
$adding->addChild('email', 'Email Here');

header('Content-Type: text/xml');
echo $doc->asXML();

Example #2

This example will load a file and write the new member to the xml file.

$filename = 'filename.xml';

/* PAIR */
$filename = file_get_contents($filename); // make the file contents a string
$doc = new SimpleXMLElement($filename); // needs a string as parameter

$adding = $doc->addChild('member'); // main multiple elements with nested info
$adding->addChild('id', '1234');
$adding->addChild('name', 'Name Here');
$adding->addChild('phone', 'Phone Here');
$adding->addChild('email', 'Email Here');

echo $doc->asXML('test.xml');