PHP | XMLWriter startAttribute()函数


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/函数.xmlwriter-start-attribute。 PHP


Made with ❤️ in Chengdu. Copyright reserved 2019-2023.

蜀ICP备20006366号-1