Sat Jun 27 2020

Create XML File

PHP Scripting2720 views

File Name: create-xml-file.php

<?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>

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.