PHP | XMLWriter setIndent() Function
The XMLWriter::setIndent() function is an inbuilt function in PHP which is used to toggle indentation on/off in the XML document which is off by default.
Syntax:
bool XMLWriter::setIndent( bool $indent )
Parameters: This function accepts a single parameter $indent which holds a boolean stating TRUE for enabling the indentation or FALSE for disabling the indentation.
Return Value: This function returns TRUE on success or FALSE on failure.
Below examples illustrate the XMLWriter::setIndent() function in PHP:
Example 1:
<?php
$writer = new XMLWriter();
$writer ->startDocument( '1.0' , 'UTF-8' );
$writer ->setIndent(true);
$writer ->startElement( 'div' );
$writer ->startElement( 'h1' );
$writer ->text( 'Indented Text' );
$writer ->endElement();
$writer ->endElement();
$writer ->endDocument();
?>
|
Output:
<?xml version="1.0" encoding="UTF-8"?>
<div>
<h1>Indented Text</h1>
</div>
Example 2:
<?php
$writer = new XMLWriter();
$writer ->startDocument( '1.0' , 'UTF-8' );
$writer ->setIndent(true);
$writer ->setIndentString( '******' );
$writer ->startElement( 'div' );
$writer ->startElement( 'p' );
$writer ->startElement( 'h1' );
$writer ->text( 'GeeksforGeeks' );
$writer ->endElement();
$writer ->endElement();
$writer ->endElement();
$writer ->endDocument();
?>
|
Output:
<?xml version="1.0" encoding="UTF-8"?>
<div>
******<p>
************<h1>GeeksforGeeks</h1>
******</p>
</div>
Last Updated :
07 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...