I needed to make a self closing tag with one attribute so I did:
<?php
$writer->startElement('CookTime');
$writer->writeAttribute('minMinute', $cook_time);
$writer->endElement();
?>
Output: <CookTime minMinute="10"/>
(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)
XMLWriter::startElement -- xmlwriter_start_element — Create start element tag
面向对象风格
$name
)过程化风格
$xmlwriter
, string $name
)Starts an element.
xmlwriter
仅用于过程调用。被修改的 XMLWriter resource。此资源来自于对 xmlwriter_open_uri() 或 xmlwriter_open_memory() 的调用。
name
The element name.
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
I needed to make a self closing tag with one attribute so I did:
<?php
$writer->startElement('CookTime');
$writer->writeAttribute('minMinute', $cook_time);
$writer->endElement();
?>
Output: <CookTime minMinute="10"/>
Note that startElement() and startElementNS() naturally do not write the closing ">" of the tag, since you may add an arbitrary number of attributes after starting a tag.
However, in some cases you may want to have the starting element tag closed in the output buffer before writing any further elements or content (e.g. if you communicate via an XML stream). To achieve this, you can simply use the text() method with an empty string.
Call trim on the tag as a trailing space breaks the end element.