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


PHP XMLWriter startAttribute()用法及代码示例


XMLWriter::startAttribute()函数是PHP中的一个内置函数,用于启动属性。稍后可以使用XMLWriter::endAttribute()函数关闭此属性。

用法:

bool XMLWriter::startAttribute( string $name )

参数:该函数接受单个参数$name,该参数保存属性的名称。



返回值:如果成功,此函数将返回TRUE,否则将返回FALSE。

以下示例说明了PHP中的XMLWriter::startAttribute()函数:

范例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'); 
  
// Start a element 
$writer->startElement('div'); 
  
// Start the attribute 
$writer->startAttribute('attrib'); 
  
// Add value to the attribute 
$writer->text('value'); 
  
// End the attribute 
$writer->endAttribute(); 
  
// End the element 
$writer->endElement(); 
  
// End the document 
$writer->endDocument(); 
?>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<div attrib="value"/>

范例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'); 
  
// Start a h1 element 
$writer->startElement('h1'); 
  
// Start the style attribute 
$writer->startAttribute('style'); 
  
// Add value to the attribute 
$writer->text('color:green'); 
  
// End the attribute 
$writer->endAttribute(); 
  
// Add value to the element 
$writer->text('GeeksforGeeks'); 
  
// End the element 
$writer->endElement(); 
  
// End the document 
$writer->endDocument(); 
?>

输出:

参考: https://www.php.net/manual/en/function.xmlwriter-start-attribute.php




相关用法


注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | XMLWriter startAttribute() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。