Core\Xml::writer
Core\Xml::writer({indent?: string}): Core\Xml\WriterCore\Xml::writer( {indent?: string}, ): Core\Xml\Writer
Returns a writer that builds a new XML document one node at a time. You call startDocument, then open elements, add attributes and text, and close the elements again. endDocument returns the finished document as a string.
The writer escapes every text and attribute value for you. A < or & in your data stays text and never becomes markup.
The writer also remembers which elements are open, so endElement needs no name. A document that is not finished is an error. endDocument throws a LogicError while an element is still open, and so does any call in the wrong order.
The option indent puts each element on its own line and indents it. It must contain only whitespace, such as two spaces.
The examples below write a small document, show how text is escaped, and export a list of orders.
Options
indentstringoptionalThe text for one level of indentation, such as two spaces. The default is"", which means no indentation. It may contain only whitespace. The writer never adds indentation next to text content, because that would change the text.
Return value
Returns Core\Xml\Writer. A new writer with an empty document. Call startDocument first.
Errors
LogicErrorindentcontains a character that is not whitespace.
Examples
Write a small document
<?nvs// Shows how to write a small XML document one node at a time.
Core\Xml\Writer $out = Core\Xml::writer();
// `startDocument` writes the XML declaration. Every other call comes after it.$out->startDocument();$out->startElement('greeting');$out->writeAttribute('lang', 'en');$out->writeText('Hello');// `endElement` closes the element that was opened last, so it needs no name.$out->endElement();
// `endDocument` returns the whole document as a string.echo $out->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><greeting lang="en">Hello</greeting>Text is escaped
<?nvs// Shows that the writer escapes text, and throws an error for a document that is not finished.
Core\Xml\Writer $out = Core\Xml::writer();$out->startDocument();$out->startElement('note');// `"`, `<`, `>` and `&` are written as entities, so they stay text and do not become markup.$out->writeAttribute('title', "Tom's \"best\" <idea>");$out->writeText('1 < 2 & 3 > 2');$out->endElement();echo $out->endDocument(), "\n";
// This writer opens an element and never closes it. `endDocument` throws a `LogicError`.Core\Xml\Writer $open = Core\Xml::writer();$open->startDocument();$open->startElement('list');try { echo $open->endDocument(), "\n";} catch (LogicError $error) { echo $error->message, "\n";}<?xml version="1.0" encoding="UTF-8"?><note title="Tom's "best" <idea>">1 < 2 & 3 > 2</note>Core\Xml\Writer::endDocument(): `<list>` is still open, so the document is not complete.Export orders
<?nvs// Shows how to export a list of orders as an XML document for another system.
array<array<string>> $orders = [ ['id' => '1001', 'customer' => 'Ada Lovelace', 'total' => '49.90'], ['id' => '1002', 'customer' => 'Smith & Sons', 'total' => '120.00'],];
// The option `indent` is the text for one level of indentation. Here it is two spaces.Core\Xml\Writer $out = Core\Xml::writer({ indent: ' ' });$out->startDocument();$out->startElement('orders');foreach ($orders as array<string> $order) { $out->startElement('order'); $out->writeAttribute('id', $order['id']); $out->startElement('customer'); $out->writeText($order['customer']); $out->endElement(); $out->startElement('total'); $out->writeText($order['total']); $out->endElement(); $out->endElement();}$out->endElement();
// This prints the document with one element on each line. `&` in "Smith & Sons" is written as `&`.echo $out->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><orders> <order id="1001"> <customer>Ada Lovelace</customer> <total>49.90</total> </order> <order id="1002"> <customer>Smith & Sons</customer> <total>120.00</total> </order></orders>