How to Create an XML Document & Root Element as a String in PHP
PHP scripts can build complex text strings using markup structures such as XML elements. You can build the content of an XML document, starting with the root element, as a single string variable. You can then use this string content to output information to website users or to write to an XML file. You can tailor the structures within the XML to the needs of particular projects, optionally including data values within the element and attribute markup.
-
Preparation
-
To build XML in PHP, you must first create a PHP script. Do this by opening a new file in a text editor and saving it with ".php" extension; for example, "write_xml.php" to match the purpose of the script. Inside the script, add the following outline structures:
<?php
//xml building processes here
?>Between the PHP tags, add all of your script processing. The following code adds a variable to hold the content of the XML document:
$xml_content = "";This creates an empty string variable. You can add XML elements to this variable as the script progresses.
Document Root
-
XML documents begin with the XML declaration. Add one to the string variable as follows:
$xml_content.="<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";This code adds the start of the XML document to the string variable, escaping the quote characters with backslash characters so that PHP does not misinterpret them. The XML declaration is followed by the opening tag for the root element. The following code demonstrates appending the opening tag of an example root element to the string variable:
$xml_content.="<kitchen>";The tag content can be altered to suit different projects.
-
Content
-
Between the opening and closing tags of an XML document root element, you normally find other XML elements as well as attributes in some cases. The following sample code demonstrates adding a few elements with attributes:
$xml_content.="<utensil type=\"appliance\">Toaster</utensil>";You can carry on adding elements to the string variable until you have all the data you need. These statements can run across multiple lines if necessary.
Closing the Root
-
The root element of an XML document must be closed. To complete the XML document string variable, close the root element as follows:
$xml_content.="</kitchen>";This is all you need for a basic XML document. All of the content a script specifies should now be stored within the string variable in order. It is advisable to check code at this stage to make sure all of the elements in your XML content are closed and correctly structured.
Output
-
PHP scripts can use the XML string variable in many possible ways. To output the XML document content to the Web browser of any user fetching the PHP script, the following syntax applies:
echo $xml_content;Test the PHP script by browsing to the page. You can also write XML content to a file using PHP's "fopen" and "fwrite" functions.
-
References
Resources
- Photo Credit Jason Reed/Photodisc/Getty Images