當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP XMLWriter setIndent()用法及代碼示例


XMLWriter::setIndent()函數是PHP中的內置函數,可用於打開/關閉XML文檔中的縮進(默認情況下處於關閉狀態)。

用法:

bool XMLWriter::setIndent( bool $indent )

參數:此函數接受單個參數$indent,該參數包含一個布爾值,表示布爾值TRUE(表示啟用縮進)或FALSE(表示禁用縮進)。



返回值:如果成功,此函數將返回TRUE,否則將返回FALSE。

以下示例說明了PHP中的XMLWriter::setIndent()函數:

範例1:

<?php 
  
// Create a new XMLWriter instance 
$writer = new XMLWriter(); 
  
// Create the output stream as PHP 
$writer->openURI('php://output'); 
  
// Start the document 
$writer->startDocument('1.0', 'UTF-8'); 
  
// Enable the indents 
$writer->setIndent(true); 
  
// Start a element 
$writer->startElement('div'); 
  
// Start a element 
$writer->startElement('h1'); 
  
// Add value to the element 
$writer->text('Indented Text'); 
  
// End the element 
$writer->endElement(); 
  
// End the element 
$writer->endElement(); 
  
// End the document 
$writer->endDocument(); 
?>

輸出:

<?xml version="1.0" encoding="UTF-8"?>
<div>
 <h1>Indented Text</h1>
</div>

範例2:

<?php 
  
// Create a new XMLWriter instance 
$writer = new XMLWriter(); 
  
// Create the output stream as PHP 
$writer->openURI('php://output'); 
  
// Start the document 
$writer->startDocument('1.0', 'UTF-8'); 
  
// Enable the indents 
$writer->setIndent(true); 
  
// Set the indent string 
$writer->setIndentString('******'); 
  
// Start a element 
$writer->startElement('div'); 
  
// Start a element 
$writer->startElement('p'); 
  
// Start a element 
$writer->startElement('h1'); 
  
// Add value to the element 
$writer->text('GeeksforGeeks'); 
  
// End the element 
$writer->endElement(); 
  
// End the element 
$writer->endElement(); 
  
// End the element 
$writer->endElement(); 
  
// End the document 
$writer->endDocument(); 
?>

輸出:

<?xml version="1.0" encoding="UTF-8"?>
<div>
******<p>
************<h1>GeeksforGeeks</h1>
******</p>
</div>

參考: https://www.php.net/manual/en/function.xmlwriter-set-indent.php




相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLWriter setIndent() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。