PHP XML formatOutput Example

PHP XML formatOutput Example

Posted in : PHP Posted on : January 21, 2011 at 6:49 PM Comments : [ 0 ]

This section contains the detail about the formatOutput in xml using PHP.

PHP XML formatOutput Example:

If user want to create a formatted xml file then formatOutput is very useful. If user set formatOutput as true then format xml file will generate if formatOutput is set false then generated xml will not formatted. The formatOutput feature doesn' t work with documents, that use DomDocument::load() for creation.

Example :


$doc_object = new DOMDocument('1.0');

$doc_object->formatOutput = true;

$root_element = $doc_object->createElement('roseindia');
$root_element = $doc_object->appendChild($root_element);

$companytype = $doc_object->createElement('companyType');
$companytype = $root_element->appendChild($companytype);

$No_Of_Emp = $doc_object->createElement('No_Of_Emp');
$No_Of_Emp = $root_element->appendChild($No_Of_Emp);

$company_type_text = $doc_object->createTextNode('IT Company');
$company_type_text = $companytype->appendChild($company_type_text);

$No_Of_Emp_text = $doc_object->createTextNode('20');
$No_Of_Emp_text = $No_Of_Emp->appendChild($No_Of_Emp_text);

echo "newxml.xml file created.";


This example will generated a formatted newxml.xml file and save it in the given location.


The browser output is:

newxml.xml file created.

and generated newxml.xml file content is:

<?xml version="1.0"?>
	<companyType>IT Company</companyType>

Download Example Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics