As we go through the articles, we would be build a small library of functions which would prove to be very handy when you would start XML development.
php_dom_xml_library.php
function createFile()
{
// create new dom document
$xml = new DOMDocument();
// save dom document to an xml file
$xml->save('out.xml');
}
// call xml function
createFile();
output:
<?xml version="1.0"?>
This very simple two line code creates and saves and XML file. The first line creates a new DOM document. The second line saves the DOM document to an XML file.
Next, we add a root element to the DOM and save it in our XML file.
php_dom_xml_library.php
function createFile()
{
// create new dom document
$xml = new DOMDocument();
// these lines would create a nicely indented XML file
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
// create a root element
$root = $xml->createElement("phonebook");
// add root to DOM document root
$xml->appendChild($root);
// save dom document to an xml file
$xml->save('out.xml');
}
// call xml function
createFile();
output:
<?xml version="1.0"?> <phonebook/>
By default the entire XML document is saved as one long string. To generate a more readable document, we need to set preserveWhiteSpace to false and formatOutput to true as in the second and third lines of code. First we create and DOM element called phonebook, then we append it to the DOM document.
Let's add a few more elements to the XML document.
php_dom_xml_library.php
function createFile($xml_file)
{
// create new dom document
$xml = new DOMDocument();
// these lines would create a nicely indented XML file
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
// create a root element, and add it to DOM
addRoot($xml);
// add more elements to xml file
$friend = $xml->createElement("friend");
// add this element to the root
$xml->documentElement->appendChild($friend);
// create two more elements
$name = $xml->createElement("name");
$phone = $xml->createElement("phone");
// append these elements to friend
$friend->appendChild($name);
$friend->appendChild($phone);
// save dom document to an xml file
$xml->save($xml_file);
}
function addRoot(&$xml)
{
$xml->appendChild($xml->createElement("phonebook"));
}
// call xml function
createFile('out.xml');
output:
<?xml version="1.0"?>
<phonebook>
<friend>
<name/>
<phone/>
</phonebook>
In this code, I have simple moved the code to create the root to another function. createFile() now take XML filename as a parameter. The important lines are between addRoot() and $xml->save(). documentElement refers to the root element. To add an element to the XML file, we need to create it and then append it to another node. The last two lines before xml_save append the created elements to the element friend.
So far, we have only been creating tags. Time to add text to the tags.
php_dom_xml_library.php
function createFile($xml_file)
{
// create new dom document
$xml = new DOMDocument();
// these lines would create a nicely indented XML file
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
// create a root element, and add it to DOM
$root = $xml->appendChild($xml->createElement("phonebook"));
// create element and add to root
$friend = $xml->createElement("friend");
$xml->documentElement->appendChild($friend);
// create elements
$name = $xml->createElement("name");
$phone = $xml->createElement("phone");
// append elements to friend
$friend->appendChild($name);
$friend->appendChild($phone);
// add text to elements
$name->appendChild($xml->createTextNode("Alice"));
$phone->appendChild($xml->createTextNode("123-456-7890"));
// save dom document to an xml file
$xml->save($xml_file);
}
// call xml function
createFile('out.xml');
output:
<?xml version="1.0"?>
<phonebook>
<friend>
<name>Alice</name>
<phone>123-456-7890</phone>
</phonebook>
Text has to be added to DOM as a node. To add text, we use createTextNode() function and append it to a DOM element.
Next we add attributes to an XML file
php_dom_xml_library.php
function createFile($xml_file)
{
// create new dom document
$xml = new DOMDocument();
// these lines would create a nicely indented XML file
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
// create a root element, and add it to DOM
$root = $xml->appendChild($xml->createElement("phonebook"));
// create element and add to root
$friend = $xml->createElement("friend");
$xml->documentElement->appendChild($friend);
// create elements
$name = $xml->createElement("name");
$phone = $xml->createElement("phone");
// append elements to friend
$friend->appendChild($name);
$friend->appendChild($phone);
// add text to elements
$name->appendChild($xml->createTextNode("Alice"));
$phone->appendChild($xml->createTextNode("123-456-7890"));
// add attributes
$phone_attr1 = $xml->createAttribute("type");
$phone_attr1->appendChild($xml->createTextNode("cell"));
$phone_attr2 = $xml->createAttribute("country_code");
$phone_attr2->appendChild($xml->createTextNode("1"));
$phone->appendChild($phone_attr1);
$phone->appendChild($phone_attr2);
// save dom document to an xml file
$xml->save($xml_file);
}
// call xml function
createFile('out.xml');
output:
<?xml version="1.0"?>
<phonebook>
<friend>
<name>Alice</name>
<phone type="cell" country_code="1">123-456-7890</phone>
</phonebook>
supra skylow
DOM beats SimpleXML