Exporters provide functionality to convert data into XML format. The package includes two exporters: ArrayExporter for converting arrays and ViewExporter for rendering Blade views.
ArrayExporter
The ArrayExporter class converts PHP arrays to XML documents. It extends XMLBuilder and implements the Exporter interface.
Constructor
new ArrayExporter(array $data)
The array data to export as XML.
use Flowgistics\XML\Exporters\ArrayExporter;
$exporter = new ArrayExporter([
'users' => [
['name' => 'John', 'email' => '[email protected]'],
['name' => 'Jane', 'email' => '[email protected]']
]
]);
Prefer using XML::export($data) instead of instantiating directly.
usePrettyOutput()
Enable formatted (pretty-printed) XML output.
public function usePrettyOutput(): self
Returns the exporter instance for chaining.
$xml = XML::export($data)
->usePrettyOutput()
->toString();
Example Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<user>
<name>John</name>
<email>[email protected]</email>
</user>
<user>
<name>Jane</name>
<email>[email protected]</email>
</user>
</root>
toString()
Convert the array to an XML string.
public function toString(?bool $prettyOutput = null): string
When true, formats the XML with indentation. Overrides usePrettyOutput() if specified.
// Basic usage
$xml = XML::export($data)->toString();
// With pretty output parameter
$xml = XML::export($data)->toString(true);
// Using method chaining
$xml = XML::export($data)
->usePrettyOutput()
->toString();
toFile()
Save the XML to a file.
public function toFile(string $path): void
The file path where the XML should be saved.
XML::export($data)
->usePrettyOutput()
->toFile(storage_path('exports/data.xml'));
The file will be created if it doesn’t exist. Existing files will be overwritten.
Complete ArrayExporter Example
use Flowgistics\XML\Facades\XML;
$users = [
'users' => [
[
'id' => 1,
'name' => 'John Doe',
'email' => '[email protected]',
'role' => 'admin'
],
[
'id' => 2,
'name' => 'Jane Smith',
'email' => '[email protected]',
'role' => 'user'
]
]
];
// Export with custom configuration
$xml = XML::export($users)
->version('1.0')
->encoding('UTF-8')
->rootTag('data')
->itemName('user')
->usePrettyOutput()
->toString();
// Save to file
XML::export($users)
->rootTag('users')
->itemName('user')
->usePrettyOutput()
->toFile('output.xml');
ViewExporter
The ViewExporter class renders Laravel Blade views as XML documents. It extends XMLBuilder and implements the Exporter interface.
Constructor
new ViewExporter(string $viewName, array $data = [])
The name of the Blade view to render.
Data to pass to the view.
use Flowgistics\XML\Exporters\ViewExporter;
$exporter = new ViewExporter('xml.products', [
'products' => $products
]);
Prefer using XML::exportView($viewName, $data) instead of instantiating directly.
toString()
Render the view and return as an XML string.
public function toString(): string
Returns the complete XML string with prolog and root tags.
$xml = XML::exportView('xml.report', ['data' => $reportData])
->toString();
toFile()
Render the view and save to a file.
public function toFile(string $path): void
The file path where the XML should be saved.
XML::exportView('xml.invoice', ['invoice' => $invoice])
->toFile(storage_path('invoices/invoice-' . $invoice->id . '.xml'));
Complete ViewExporter Example
Controller
use Flowgistics\XML\Facades\XML;
use App\Models\Product;
class ProductExportController extends Controller
{
public function export()
{
$products = Product::all();
return response()
->make(
XML::exportView('xml.products', compact('products'))
->rootTag('catalog')
->toString(),
200,
['Content-Type' => 'application/xml']
);
}
public function exportToFile()
{
$products = Product::all();
XML::exportView('xml.products', compact('products'))
->rootTag('catalog')
->toFile(storage_path('exports/products.xml'));
return response()->download(storage_path('exports/products.xml'));
}
}
Blade View (resources/views/xml/products.blade.php)
@foreach($products as $product)
<product id="{{ $product->id }}">
<name>{{ $product->name }}</name>
<description>{{ $product->description }}</description>
<price currency="USD">{{ $product->price }}</price>
<stock>{{ $product->stock }}</stock>
@if($product->categories->count())
<categories>
@foreach($product->categories as $category)
<category>{{ $category->name }}</category>
@endforeach
</categories>
@endif
</product>
@endforeach
Generated XML Output
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<product id="1">
<name>Laptop</name>
<description>High-performance laptop</description>
<price currency="USD">999.99</price>
<stock>50</stock>
<categories>
<category>Electronics</category>
<category>Computers</category>
</categories>
</product>
<product id="2">
<name>Mouse</name>
<description>Wireless mouse</description>
<price currency="USD">29.99</price>
<stock>200</stock>
<categories>
<category>Electronics</category>
<category>Accessories</category>
</categories>
</product>
</catalog>
Exporter Interface
Both exporters implement the Exporter interface with the following methods:
interface Exporter
{
public function toString(): string;
public function toFile(string $path): void;
}
Inherited Methods
Both exporters extend XMLBuilder, so they inherit all configuration methods:
version(string $version) - Set XML version
encoding(string $encoding) - Set character encoding
rootTag(string $name) - Set root tag name
itemName(string $name) - Set default item name
disableRootTag() - Disable root tag wrapper
forceItemName() - Force item name usage
See the XMLBuilder documentation for details.
Comparison
| Feature | ArrayExporter | ViewExporter |
|---|
| Input | PHP Array | Blade View |
| Pretty Output | usePrettyOutput() | Manual formatting in view |
| Dynamic Content | Limited | Full Blade features |
| Complexity | Simple arrays | Complex structures |
| Best For | API responses, simple exports | Reports, invoices, complex layouts |
Best Practices
Choose the right exporter:
- Use
ArrayExporter for simple, structured data
- Use
ViewExporter for complex layouts or when you need Blade’s templating features
XML Validation:
Neither exporter validates the resulting XML. Ensure your data or views produce valid XML structure.
Performance:
For large datasets, ArrayExporter is generally faster. For complex formatting, ViewExporter provides better maintainability.