Reading Time: 2 minutes

Handling XML files using PHP is very simple, as PHP provides built-in functions to create and retrieve XML elements. In this tutorial, you will learn how to create a new XML element and how to retrieve elements from an XML file using PHP’s DOM functions.

Creating an XML Element

To create a new element in an XML file, you use the createElement() function. The name of the element is passed as a parameter, and you may also provide a value for that element.

Note that creating an element alone does not insert it into the XML document. To make it part of the XML structure, you must use the appendChild() function. If an error occurs while creating the element, the function returns FALSE.

Consider the following XML file candidate.xml:


<?xml version="1.0" encoding="UTF-8"?>
<candidate>
  <candidatename>Robert</candidatename>
  <candidatecity>New York</candidatecity>
  <SSN>20072007</SSN>
  <organization>XYZ Corp</organization>
</candidate>

We will add a new element called candidatedesignation to this XML file.

PHP Code to Create a New XML Element


<?php
$xdoc = new DomDocument;
$xdoc->load('C:/php/xml_files/candidate.xml');

$candidate = $xdoc->getElementsByTagName('candidate')->item(0);
$newElement = $xdoc->createElement('candidatedesignation');

$candidate->appendChild($newElement);
$xdoc->save("C:/php/xml_files/candidate2.xml");

echo "<b>New Element Created</b>";
?>

After executing the above code, the XML file will be updated as shown below:


<?xml version="1.0" encoding="UTF-8"?>
<candidate>
  <candidatename>Robert</candidatename>
  <candidatecity>New York</candidatecity>
  <SSN>20072007</SSN>
  <organization>XYZ Corp</organization>
  <candidatedesignation />
</candidate>

If you want to add text inside the newly created element, you can use the createTextNode() function.


Getting Elements from an XML File

To retrieve elements from an XML file, PHP provides the getElementsByTagName() function. This function accepts the name of the element to search for and returns a DOMNodeList containing all matching elements.

To retrieve all elements in the XML document, you can use an asterisk (*) as the parameter.

Consider the following candidate.xml file:


<?xml version="1.0" encoding="UTF-8"?>
<candidate>
  <candidatename id="candid">Robert</candidatename>
  <candidatecity>New York</candidatecity>
  <SSN>20072007</SSN>
  <organization>XYZ Corp</organization>
</candidate>

From this XML file, we will retrieve the candidatename element and read the value of its id attribute.

PHP Code to Get an XML Element and Its Attribute


<?php
$xdoc = new DomDocument;
$xdoc->load('C:/php/xml_files/candidate.xml');

$candidatename = $xdoc->getElementsByTagName('candidatename')->item(0);

echo "<html><head>";
echo "<title>Getting Elements By Tag Name</title>";
echo "</head><body><b>";
echo "Attribute Name is: " . $candidatename->getAttribute('id');
echo "</b></body></html>";
?>

Using these PHP functions, you can easily create XML elements and retrieve data from XML files. The examples above demonstrate how simple and efficient it is to work with XML using PHP.