PHP Scripting

Create XML File

Create XML file using PHP SimpleXMLElement

6/27/2020
0 views
create-xml-file.phpPHP
<?php
	/* Create object of 'SimpleXMLElement' to represents an element in an XML document. */
	$xml = new SimpleXMLElement('<xml/>');
	
	/* Adds a child element to the XML node */
	$customer = $xml->addChild('customer');
	
	/* Adds a child elements to the customer node */
	$customer->addChild("name", "James");
	$customer->addChild("email", "james@gmail.com");
	$customer->addChild("requirement", "Website");
	
	/* Adds a child element to the XML node */
	$customer = $xml->addChild('customer');
	
	/* Adds a child elements to the customer node */
	$customer->addChild("name", "Jonny");
	$customer->addChild("email", "jonny@gmail.com");
	$customer->addChild("requirement", "Android App");
	
	/* Set header */
	header('Content-type: text/xml; charset=utf-8');
	
	/* Put all the xml contents in a file */
	file_put_contents("customers.xml",$xml->asXML());
	
	echo "Xml file created successfully!";
?>




/* Output */
<?xml version="1.0"?>
<xml>
	<customer>
		<name>James</name>
		<email>james@gmail.com</email>
		<requirement>Website</requirement>
	</customer>

	<customer>
		<name>Jonny</name>
		<email>jonny@gmail.com</email>
		<requirement>Android App</requirement>
	</customer>
</xml>
PHPcreate XML file using PHPSimpleXMLElement

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com