Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Array To Xml Laravel Package

spatie/array-to-xml

Simple PHP utility to convert arrays into XML strings. Supports custom root element names and configurable key handling (e.g., converting spaces to underscores). Ideal for quickly generating XML output from structured array data.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require spatie/array-to-xml. No Laravel service provider registration is needed—it’s a plain PHP utility. The core usage is straightforward:

use Spatie\ArrayToXml\ArrayToXml;

$xml = ArrayToXml::convert([
    'user' => [
        '@attributes' => ['id' => 123],
        'name' => 'Jane Doe',
        'email' => 'jane@example.com',
    ]
], 'response');

echo $xml; // Outputs valid XML with <response> root

The first example in the README covers the most common pattern—converting a simple nested array to XML—and is the best place to begin. For Laravel users, drop it into a controller method, job, or console command where XML generation is required (e.g., sitemap generation or legacy API responses).

Implementation Patterns

  • Route/Controller Usage: Use in API endpoints that must return XML (e.g., Feeds, XML-RPC, or EDI integrations):
    return response($xml)
        ->header('Content-Type', 'application/xml');
    
  • ** queued Jobs/Commands**: Generate large XML files (e.g., product feeds) asynchronously. Stream output using ArrayToXml::convert([...], 'root', null, false) with echo inside a command.
  • Laravel Collections: Bridge collections to XML using collect([...])->toArray() as input.
  • Dynamic Structuring: Leverage @attributes, @value, and @cdata magic keys to embed metadata and unescaped content:
    'product' => [
        '@attributes' => ['sku' => 'ABC'],
        '@cdata' => '<description>Raw <b>HTML</b></description>', // or use `@value` for plain text
    ]
    
  • Customizing Output: Override default root element name, add XML declaration, or tweak encoding:
    ArrayToXml::convert($data, 'inventory', 'utf-8', false, '1.1');
    
  • Testing: Use in feature tests to assert exact XML structure; compare against expected fixtures.

Gotchas and Tips

  • Key collisions: Avoid using reserved keys like @attributes, @value, or @cdata as regular array keys—they’re treated as special. If needed, escape them (e.g., product@attributes) or preprocess arrays.
  • Empty arrays: Empty arrays render as self-closing tags (e.g., <items/>). If you need <items></items>, manually add a null or placeholder value.
  • Performance: For very large datasets (10k+ nodes), avoid pretty-printing ($pretty = true) in production—disabling it ($pretty = false) significantly speeds up generation.
  • Encoding: Always sanitize UTF-8 input. Non-UTF-8 characters will cause silent failures or invalid XML unless manually stripped/converted.
  • Namespace support: Namespaces (@namespace, @ns) are supported but poorly documented—refer to the unit tests for examples if needed.
  • No validation: The package produces well-formed XML but does not validate against schemas. Use external tools (e.g., xmllint) in CI if schema compliance is critical.
  • Laravel 10+ tip: No xml response helper is needed—just return the XML string with explicit application/xml header.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport