Skip to main content
The XML facade provides a simple interface for importing and exporting XML data in Laravel applications.

Import Method

Import XML data from a file path or URL.
XML::import(string $path, bool $raw = false): XMLCollection|XMLElement
path
string
required
The path to the XML file or a URL. Can be a local file path or a remote URL.
raw
bool
default:"false"
When set to true, returns raw XMLElement instead of processed XMLCollection.
return
XMLCollection|XMLElement
Returns an XMLCollection instance by default, or XMLElement if $raw is true.

Basic Usage

use Flowgistics\XML\Facades\XML;

// Import from local file
$xml = XML::import('path/to/file.xml');

// Import from URL
$xml = XML::import('https://example.com/data.xml');

// Get raw XML element
$raw = XML::import('path/to/file.xml', true);
The import method automatically detects whether the path is a URL or local file.

Export Method

Export an array to XML format.
XML::export(array $data): ArrayExporter
data
array
required
The array data to export as XML.
return
ArrayExporter
Returns an ArrayExporter instance that can be converted to string or saved to file.

Basic Usage

$data = [
    'users' => [
        ['name' => 'John', 'email' => '[email protected]'],
        ['name' => 'Jane', 'email' => '[email protected]'],
    ]
];

// Export to string
$xml = XML::export($data)->toString();

// Export to file
XML::export($data)->toFile('output.xml');

// With pretty formatting
$xml = XML::export($data)
    ->usePrettyOutput()
    ->toString();

Customizing Export

XML::export($data)
    ->rootTag('data')
    ->encoding('UTF-8')
    ->version('1.0')
    ->itemName('record')
    ->toString();

Export View Method

Export a Laravel Blade view as XML.
XML::exportView(string $viewName, array $data = []): ViewExporter
viewName
string
required
The name of the Blade view to render as XML.
data
array
default:"[]"
Data to pass to the view.
return
ViewExporter
Returns a ViewExporter instance that can be converted to string or saved to file.

Basic Usage

// Export view to string
$xml = XML::exportView('xml.template', ['users' => $users])->toString();

// Export view to file
XML::exportView('xml.products', ['products' => $products])
    ->toFile('products.xml');

// Customize root tag
XML::exportView('xml.report', $data)
    ->rootTag('report')
    ->toString();

Example View

@foreach($users as $user)
    <user>
        <name>{{ $user->name }}</name>
        <email>{{ $user->email }}</email>
    </user>
@endforeach
Views are automatically wrapped with XML prolog and root tags. You only need to define the content.

Constants

The XML facade provides optimization constants for use with XMLCollection:
XML::OPTIMIZE_UNDERSCORE  // Convert keys to snake_case
XML::OPTIMIZE_CAMELCASE   // Convert keys to camelCase
XML::OPTIMIZE_NONE        // No key transformation

Usage with XMLCollection

$xml = XML::import('data.xml')
    ->optimize(XML::OPTIMIZE_UNDERSCORE)
    ->get();

Build docs developers (and LLMs) love