当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。