- How do I install mtownsend/xml-to-array in a Laravel app?
- Run composer require mtownsend/xml-to-array. Laravel package auto-discovery registers the service provider automatically, so there is nothing to add to config/app.php on supported versions. The only runtime dependency is ext-simplexml, which is enabled by default in virtually every PHP build. There is no database, queue, or publishable config involved, so installation is effectively one command.
- Can I use mtownsend/xml-to-array without Laravel?
- Yes. The package is a thin, stateless wrapper over PHP's SimpleXML extension and works in any Composer project, including legacy non-framework code you are incrementally modernizing. Plain-PHP usage via a static call works without booting the framework. That makes it easy to drop into an ingestion script or a one-off import job with no architectural commitment.
- Which PHP and Laravel versions does xml-to-array support?
- The package targets modern stacks and fits Laravel 8 through 12 on PHP 8.1 or newer. The only hard runtime requirement is ext-simplexml, so it also runs fine in framework-free Composer projects. Always check the composer.json constraints for the exact illuminate/support range before a major Laravel upgrade, since constraint ranges are the authoritative answer.
- Why does an XML element sometimes become a string and sometimes an array?
- This is the classic XML-to-array footgun rather than a bug in the package: an element with a single child yields a scalar, while the same element with two children yields an array. Consumers written against one shape break when a payload arrives with the other. Normalize the result immediately after parsing, or map it into a DTO so the rest of your code never sees the raw shape. This inherent instability is the main thing to plan for in production.
- How are XML attributes represented and can I rename the @attributes key?
- Attributes and text nodes are mapped to reserved keys such as @attributes and @value, which is a lossy but predictable convention. A real element literally named @attributes would collide, which is rare but legal XML. Check the README and source for whether that key name is configurable and whether a documented escaping rule exists for collisions. If you need full fidelity between attributes and text, this package is the wrong layer.
- Is mtownsend/xml-to-array safe for parsing untrusted XML?
- Treat that as something to verify rather than assume: confirm the implementation does not pass LIBXML_NOENT and does pass LIBXML_NONET. Modern libxml disables network entity loading by default, but a default is not a control, so read the source. Entity-expansion attacks such as billion laughs depend on libxml behaviour and version, so cap input size upstream regardless. For hostile or externally sourced payloads, a dedicated hardened parser may be the safer choice.
- Can I stream or parse very large XML files with xml-to-array?
- No. The whole document is materialized in memory and converted into a nested PHP array, so memory use scales with payload size. Streaming ingestion, huge partner feeds, and multi-hundred-megabyte exports should use XMLReader or a streaming parser instead. This package is designed for webhook payloads, API responses, feeds, and config or seed files that comfortably fit in memory.
- What exception is thrown when xml-to-array receives malformed XML?
- Invalid XML triggers an exception, but you should check the source to see whether it is a generic Exception or a typed one, since that determines how narrowly callers can catch it. It is also worth confirming whether libxml warnings are surfaced or suppressed, because silent failures are harder to debug in an ingestion pipeline. Wrap parsing calls defensively at the boundary and log the offending payload. Also note that empty, self-closing, and absent elements are not reliably distinguishable in the resulting array.
- How do I test XML-to-array parsing in a Laravel test suite?
- Store representative sample payloads as fixtures, including edge cases like single-child elements, repeated siblings with differing attributes, CDATA sections, namespaces, and self-closing tags. Assert the exact array shape your mapper produces rather than the raw library output, so a change in shape fails loudly. Because the package is stateless and has no container coupling, these are fast unit tests with no database or framework bootstrapping needed. Realistic effort for a full test suite is under a day once sample payloads are available.
- When should I use something other than xml-to-array for XML ingestion?
- Avoid it for streaming or very large documents, XSD-validated ingestion, XSLT transformation, and mixed-content document models where element order and interleaved text are semantically meaningful. It is also the wrong layer if your downstream logic needs to distinguish an empty element from a missing one. For everything else — webhook payloads, legacy SOAP or REST feeds, config and seed loading, admin-uploaded XML previews — it is a clean, discardable adapter. Because it returns an untyped array, always feed a mapper or DTO layer rather than your domain objects directly.