Skip to content
Thomas Weinert edited this page Aug 19, 2017 · 2 revisions

FluentDOM\XMLWriter

FluentDOM\XMLWriter extends XMLWriter with improved namespace support. Most of it is to work around a bug that adds unnecessary namespace nodes.

FluentDOM\XMLWriter::registerNamespace()

public function registerNamespace(string $prefix, string $namespace)

This method registers a namespace on the XMLReader itself. This allows other methods to resolve prefixes in tag name arguments.

Example

$writer = new FluentDOM\XMLWriter();
$writer->openURI('php://stdout');
$writer->registerNamespace('', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$writer->registerNamespace('video', 'http://www.google.com/schemas/sitemap-video/1.1');

$writer->setIndent(2);
$writer->startDocument();
$writer->startElement('urlset');
$writer->writeAttribute('xmlns:video', 'http://www.google.com/schemas/sitemap-video/1.1');

foreach ($videos as $video) {
  $writer->startElement('url');
  $writer->writeElement('loc', $video['url']);
  $writer->startElement('video:video');
  $writer->writeElement('video:title', $video['title']);
  $writer->endElement();
  $writer->endElement();
}
$writer->endElement();
$writer->endDocument();

FluentDOM\XMLWriter::collapse()

public function collapse(\DOMNode|\Traversable|array $nodes, int $maximumDepth = 1000)

This method allows you to write a DOM node or a list of DOM nodes into the XMLWriter output. In some regard it is the opposite of XMLReader::expand().

The second argument allows to limit the recursion depth.

Clone this wiki locally