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 :
<?php $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); $doc_object->save("newxml.xml"); echo "newxml.xml file created."; ?>
This example will generated a formatted newxml.xml file and save it in the given location.
Output:
The browser output is:
newxml.xml file created.
and generated newxml.xml file content is:
<?xml version="1.0"?> <roseindia> <companyType>IT Company</companyType> <No_Of_Emp>20</No_Of_Emp> </roseindia>
[ 0 ] Comments