XML stands for Extensible Markup Language which is used for store and transport data. Some web project needs to store products, user, or other information in the XML file. In those projects, you should need to generate XML data or convert PHP array to XML, then create and save the XML file using PHP. Also, XML format is commonly used in sitemap of the website.
In this tutorial, we’ll show the simple way to generate XML file using PHP. You can use XML DOM Parser to process XML document in PHP. Also, using saveXml()
and save()
method you’ll be able to output XML document to the browser and save XML document as a file.
saveXml()
function puts internal XML document into a string.save()
function puts internal XML document into a file.The following example PHP code will create XML sitemap and save XML file into the xml/
directory.
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.codexworld.com</loc>
<lastmod>2016-07-04T07:46:18+00:00</lastmod>
<changefreq>always</changefreq>
<priority>1.00</priority>
</url>
</urlset>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
//Save XML as a file
$dom->save('xml/sitemap.xml');
Use the following PHP code to output XML document on the browser.
//View XML document
$dom->formatOutput = TRUE;
echo $dom->saveXml();
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Very easy to work! Thanks
This is working and easy to use.. Thanks!!!
Thanks a lot
it works for me