thenorthmemory/xml
Lightweight XML transformer for PHP: parse XML into arrays and build XML back from arrays. Supports repeated elements/lists, optional pretty printing, custom root nodes, and wrapping arrays to control tag output. Extracted from wechatpay-php for general use.
SimpleXML/DOMDocument, this package offers a simpler, more opinionated API for common use cases (e.g., handling repeated tags like <item> arrays). It reduces boilerplate in service layers where XML ↔ array conversions are frequent.toArray() and toXml() map naturally to Laravel’s service layer patterns, enabling clean separation of concerns (e.g., parsing API responses in a PaymentGatewayService).<item> wrappers), reducing the need for post-processing in Laravel controllers or jobs.DOMDocument or XMLSchema, this package does not validate XML against schemas, which could be a risk for strict integrations.SimpleXML/DOMDocument for large XML payloads (e.g., 10MB+) or high-throughput scenarios (e.g., batch processing)?spatie/xml-to-array (which has 1.5K stars) better suit the project’s needs? Does this package’s wrap() feature for repeated tags justify the switch?<tag attr="value">)? The README examples focus on text nodes.simplexml_load_string, xml_parser_create, or manual string manipulation).app/Services/XmlTransformer.php) to encapsulate Transformer usage.Transformer::toArray() and test for equivalence.Transformer::toXml() with custom roots/indentation.Transformer methods using XML/array pairs from production data.libxml and simplexml (enabled by default in Laravel).SimpleXML but may reduce the need for custom parsers in most cases.<tag attr="value">content</tag> → ['@attributes' => ['attr' => 'value'], '#text' => 'content']).composer require thenorthmemory/xml
namespace App\Services;
use TheNorthMemory\Xml\Transformer;
class XmlService {
public function parse(string $xml): array {
return Transformer::toArray($xml);
}
public function build(array $data, string $root = 'xml'): string {
return Transformer::toXml($data, false, true, $root);
}
public function sanitize(string $xml): string {
return Transformer::sanitize($xml);
}
}
XmlService and use its methods.// Before
$xml = '<root><item>value</item></root>';
$array = json_decode(json_encode(simplexml_load_string($xml)), true);
// After
$array = app(XmlService::class)->parse($xml);
XmlService with real-world XML samples (e.g., from payment gateways or legacy systems).spatie/xml-to-array).dd() helpers for XML inspection).php-xml tags).spatie/xml-to-array if needed.SimpleXML/DOMDocument for large payloads.How can I help you explore Laravel packages today?