Installation:
composer require ajtis/xml-bundle
Register the bundle in config/bundles.php (Laravel) or AppKernel.php (Symfony):
// config/bundles.php
return [
// ...
Ajtis\XmlBundle\XmlBundle::class => ['all' => true],
];
First Use Case: Convert an array to XML (simplest form):
use Ajtis\XmlBundle\Model\XmlGenerator;
$generator = new XmlGenerator();
$xml = $generator->generateFromArray(['root' => ['child' => 'value']]);
Where to Look First:
vendor/ajtis/xml-bundle/docs/ for usage examples.xml_generator, xml_reader, xml_prepare).Usage section in the README for common patterns.Array-to-XML Conversion:
XmlGenerator for structured XML output with attributes, namespaces, and values.$params = [
'Request' => [
'@attrib' => ['Id' => 123],
'Data' => ['@value' => 'content']
]
];
$xml = $generator->generateFromArray($params);
XML-to-Array Parsing:
XmlReader to parse XML strings into associative arrays.$xmlString = '<root><item>value</item></root>';
$array = $reader->processConvert($xmlString);
Preparing Data:
XmlPrepare to sanitize arrays before conversion (e.g., flattening or cleaning keys).$prepared = $prepare->prepareArrayBeforeToXmlConvert($rawArray);
$xml = $generator->generateFromArray($prepared);
Laravel Service Providers:
Bind the bundle’s services to Laravel’s container in AppServiceProvider:
public function register()
{
$this->app->bind('xml_generator', function ($app) {
return new \Ajtis\XmlBundle\Model\XmlGenerator();
});
}
API Responses: Use the bundle to generate XML responses for legacy systems:
return response($generator->generateFromArray($data))->header('Content-Type', 'application/xml');
Configuration: Extend the bundle’s behavior via dependency injection (e.g., custom namespace handlers).
Namespace Handling:
http// vs. http://) will break XML validation.Symfony\Component\Validator\Constraints\Url.Attribute vs. Value Confusion:
@attrib define attributes, while @value defines text content.@value will omit text nodes in the output.Root Node Defaults:
<root> tag. Use setRootName() to customize:
$generator->setRootName('api_response');
XML Declaration:
<?xml version="1.0" encoding="UTF-8"?>.Validate XML:
Use SimpleXMLElement to debug malformed XML:
$xml = simplexml_load_string($generatedXml);
if ($xml === false) {
throw new \RuntimeException('Invalid XML: ' . libxml_get_last_error());
}
Logging:
Enable Symfony’s profiler (APP_DEBUG=true) to inspect service calls.
Custom Generators:
Extend XmlGenerator to add domain-specific logic:
class CustomXmlGenerator extends XmlGenerator {
public function generateWithCustomLogic(array $data) {
// Pre-process data
return parent::generateFromArray($data);
}
}
Event Listeners: Hook into the bundle’s lifecycle (if it supports events) to modify XML generation dynamically.
Configuration Overrides:
Override default settings via bundle configuration (check config/packages/ajtis_xml.yaml if available).
$generator->setStreaming(true);
How can I help you explore Laravel packages today?