Content · Programming · PHP

PHP XMLWriter::writeRaw() Method: Usage and Examples

Writes raw, unescaped XML markup that the caller must ensure is trusted. Learn how to apply it with examples and common mistakes.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

A common use for XMLWriter::writeRaw is processing data without rebuilding the operation manually. Writes raw, unescaped XML markup that the caller must ensure is trusted.

What Is XMLWriter::writeRaw in PHP?


Writes raw, unescaped XML markup that the caller must ensure is trusted.

Syntax:

XMLWriter::writeRaw($content)

Parameters and Return Value


  • $content — Text or markup to write.

Check the returned value, including false when applicable, or handle exceptions for invalid input. The required XML/DOM extension must be installed.

Example 1: Basic Usage


This example demonstrates the main operation with a small, controlled input.

<?php

$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument("1.0", "UTF-8");
$xmlWriter->startElement("catalog");
$trustedMarkup = "<item>Guide</item>";
$xmlWriter->writeRaw($trustedMarkup);
$xmlWriter->endElement();
echo $xmlWriter->outputMemory();

The result depends on the supplied data and the documented operation. Output is not shown as a fixed value when files, locales or runtime configuration can change it.

Example 2: Another Input


This example changes the input or surrounding setup to illustrate another behavior.

<?php

$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument("1.0", "UTF-8");
$xmlWriter->startElement("catalog");
$trustedMarkup = "<item>Updated guide</item>";
$xmlWriter->writeRaw($trustedMarkup);
$xmlWriter->endElement();
echo $xmlWriter->outputMemory();

The result depends on the supplied data and the documented operation. Output is not shown as a fixed value when files, locales or runtime configuration can change it.

Practical Example


This application-style example shows a self-contained usage pattern and checks its result.

<?php

$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument("1.0", "UTF-8");
$xmlWriter->startElement("inventory");
$trustedMarkup = "<item>Inventory item</item>";
$xmlWriter->writeRaw($trustedMarkup);
$xmlWriter->endElement();
echo $xmlWriter->outputMemory();

The result depends on the supplied data and the documented operation. Output is not shown as a fixed value when files, locales or runtime configuration can change it.

Important Behavior and Edge Cases


  • Prefer text(), writeElement(), and writeAttribute() for escaped output. Never feed untrusted markup to writeRaw(). Ensure elements and documents are closed correctly.
  • Use the intended input type and handle empty or malformed values explicitly.
  • Where this API requires an extension, version, or file, document that dependency before using it in a real application.

Comparison With Related Functions


Function or MethodWhen to Use It
XMLWriter::writeRawWrites raw, unescaped XML markup that the caller must ensure is trusted.
XMLWriter::writeElementWrites an entire element containing text in one call.

Common Beginner Mistakes


MistakeCorrect Approach
Using this API without checking the required input or extensionPrefer text(), writeElement(), and writeAttribute() for escaped output. Never feed untrusted markup to writeRaw(). Ensure elements and documents are closed correctly.
Leaving XML elements unfinishedMatch startElement() with endElement(), then finalize or flush the document.
Inserting untrusted strings with writeRaw()Prefer text(), writeElement() or writeAttribute() to escape the appropriate XML context.

Key Takeaways


  • Writes raw, unescaped XML markup that the caller must ensure is trusted.
  • Prefer text(), writeElement(), and writeAttribute() for escaped output. Never feed untrusted markup to writeRaw(). Ensure elements and documents are closed correctly.
  • Test both the expected case and at least one failure or empty-input case before deployment.

Writes raw, unescaped XML markup that the caller must ensure is trusted. Use it when the operation fits your input, output and validation requirements.

Share this resourceSend this page to someone who may find it useful.