How to Create an XML Document with PHP


In this article, we show how to create an XML document with PHP.

This means that we use PHP code to write the contents of an XML document.

This can be done in actually a few ways.

The 2 most popular ways to write XML code is through the SimpleXML extension or the DOM extension.

Both are effective ways of creating XML content via PHP.

The DOM can be described as more powerful and complex, while SimpleXML is simpler.

Below we show how to create an XML document using PHP through the SimpleXML extension.

So the first thing we do in this code is to create the $simplexml variable. This creates a new XML element, where we declare the document to be an XML document of version 1.0. We then create the root elment, books.

We then create the first element within the root element, book. We create a variable named $book1 and add the element, book. Within this book element, we add the booktitle element with the contents in between the booktitle tags being "The Wandering Oz". We then add the publicationdate element with the contents in between the publicationdate tags being 2007.

We then go onto the next book element. We again add the child element book. To this book element we add the booktitle element with the contents of "The Roaming Fox". We then add the publicationdate element with the contents being 2009.

We then create one last book element. We add a booktitle element with the contnets of "The Dominant Lion" and a publicationdate element with the contents being 2012.

We then echo out all of these XML instructions through the statement, $simplexml->asXML().

This displays all of the tags we entered and actually renders it as XML.

Being that the XML declaration has to go to the top of the page of a web document, I can't show how it would render by placing the PHP below. Instead, I have to create a new PHP file with solely this code in it. That way, the XML declaration is at the top of the page.

Doing so yields the following PHP page: books.php. ?>

Creating a Separate XML Document

The following code above is great and generates XML. However, the XML data is generated on a PHP page. This is probably not what you want. However, you more than likely want XML generated on a complete and separate XML page. This way, it's just as if you created an XML document (with .xml extension) and wrote it complete with XML tags.

To do this, the following code above, just needs a slight modification.

We add in the file_put_contents() function into the code and into this function we create a separate XML file called books.xml.

This is all shown below.

So you can see now we have most of the code we previously had, but instead of echoing the contents of the XML tags we wrote onto the current PHP page, we use the file_put_contents() function to create an XML file named books.xml. We then write the contents of the XML tags we wrote into this books.xml file.

So we now have a separate XML document. The following link shows this XML document: books.xml.

You can see how it's a perfect-looking XML document that has been generated through PHP.

This is the power of PHP and XML. PHP has built-in code that allows you to work very well with XML.

The advantage of using this code is that you generate dynamic XML on the fly with PHP. You don't simply have to write XML code. XML code can be generated dynamically through a dynamic language such as PHP.

Related Resources

How to Read JSON with PHP

HTML Comment Box is loading comments...