Now cast XML elements directly to Note model instances:
use App\Models\Note;use XML;$data = XML::import('notes.xml') ->cast('note')->to(Note::class) ->get();// Each note is now a Note model instanceforeach ($data->note as $note) { echo $note->to; // Access as model properties echo $note->completed_at->format('Y-m-d'); // Use Laravel's date casting}
The cast() method works with arrays of elements automatically. If note contains multiple items, each one will be cast to a Note instance.
Transformers let you modify XML data before using it. The built-in array transformer ensures an element is always an array:
$data = XML::import('notes.xml') ->expect('note')->as('array') ->get();// $data->note is now always an array, even if XML contains a single noteforeach ($data->note as $note) { // Process notes}
Create custom transformers for advanced filtering. This example filters only completed notes:
use Flowgistics\XML\Transformers\Transformer;use Flowgistics\XML\Data\XMLElement;class CompletedNotesFilter implements Transformer{ public static function apply($data) { return array_filter($data, function ($note) { return $note->attribute('completed') === 'true'; }); }}
Apply your custom transformer:
$completedNotes = XML::import('notes.xml') ->transform('note')->with(CompletedNotesFilter::class) ->get();// $completedNotes->note now contains only notes with completed="true"