Skip to main content
The XMLCollection class provides a powerful interface for working with imported XML data. It implements ArrayAccess, Countable, IteratorAggregate, and JsonSerializable interfaces.

Constructor

new XMLCollection(XMLElement $items)
items
XMLElement
required
The XML element to wrap in a collection.
You typically don’t instantiate XMLCollection directly. Use XML::import() instead.

Core Methods

raw()

Get the raw, unprocessed XML data.
public function raw(): XMLElement
return
XMLElement
Returns the original XMLElement instance.
$xml = XML::import('data.xml');
$rawElement = $xml->raw();

collect()

Convert the XML data to a Laravel Collection.
public function collect(): Collection
return
Collection
Returns an Illuminate\Support\Collection instance.
$collection = $xml->collect();
$filtered = $collection->filter(fn($item) => $item->active);

get()

Get the processed XML data with transformations applied.
public function get(): XMLObject|array
return
XMLObject|array
Returns the processed data as XMLObject or array.
$data = $xml->transform('users')->with('App\Transformers\UserTransformer')
    ->get();

getAsArray()

Get the XML data as a plain array.
public function getAsArray(): array
return
array
Returns the data as a plain PHP array.
$array = $xml->getAsArray();

toArray()

Convert the collection to a plain array with nested conversions.
public function toArray(): array
return
array
Returns the collection as an array with all nested items converted.
$plainArray = $xml->toArray();

toJson()

Convert the collection to JSON.
public function toJson(int $options = 0): string
options
int
default:"0"
JSON encoding options (same as json_encode()).
return
string
Returns the JSON string representation.
$json = $xml->toJson(JSON_PRETTY_PRINT);

Transformation Methods

transform()

Start a transformation for a specific key.
public function transform(string $key): PendingTransform
key
string
required
The key to transform.
return
PendingTransform
Returns a PendingTransform instance for chaining.
$xml->transform('users')
    ->with('App\Transformers\UserTransformer')
    ->get();

// Using callable
$xml->transform('price')
    ->with(fn($value) => (float) $value)
    ->get();

expect()

Alias for the transform() method.
public function expect(string $key): PendingTransform
key
string
required
The key to transform.
return
PendingTransform
Returns a PendingTransform instance.
$xml->expect('users')
    ->with('App\Transformers\UserTransformer');

cast()

Cast a key to a specific class or type.
public function cast(string $key): PendingCast
key
string
required
The key to cast.
return
PendingCast
Returns a PendingCast instance for chaining.
$xml->cast('user')->to(User::class);
$xml->cast('users')->to(User::class); // Works with arrays too

Optimization Methods

optimize()

Apply key name optimization to the XML data.
public function optimize(string $type = XML::OPTIMIZE_UNDERSCORE): self
type
string
default:"XML::OPTIMIZE_UNDERSCORE"
The optimization type. Options:
  • XML::OPTIMIZE_UNDERSCORE - Convert to snake_case
  • XML::OPTIMIZE_CAMELCASE - Convert to camelCase
  • XML::OPTIMIZE_NONE - No transformation
return
self
Returns the collection instance for chaining.
use Flowgistics\XML\XML;

// Convert keys to snake_case
$xml->optimize(XML::OPTIMIZE_UNDERSCORE)->get();

// Convert keys to camelCase
$xml->optimize(XML::OPTIMIZE_CAMELCASE)->get();

// No optimization
$xml->optimize(XML::OPTIMIZE_NONE)->get();
Optimization is applied recursively to all nested keys in the XML structure.

Array Access Methods

XMLCollection implements ArrayAccess, allowing you to use array syntax:

Array Access Examples

// Check if key exists
if (isset($xml['users'])) {
    // Key exists
}

// Get value
$users = $xml['users'];

// Set value
$xml['users'] = $newUsers;

// Unset value
unset($xml['users']);

offsetExists()

public function offsetExists(mixed $offset): bool

offsetGet()

public function offsetGet(mixed $offset): mixed

offsetSet()

public function offsetSet(mixed $offset, mixed $value): void

offsetUnset()

public function offsetUnset(mixed $offset): void

Magic Methods

__get()

Get an item from the XML using property access.
public function __get(string $key): mixed
$users = $xml->users;

__set()

Set an item in the XML using property access.
public function __set(string $name, mixed $value): void
$xml->users = $newUsers;

__isset()

Check if an item exists using property access.
public function __isset(string $name): bool
if (isset($xml->users)) {
    // Property exists
}

__call()

Forward method calls to the underlying XMLObject.
public function __call(string $name, array $arguments): mixed
// Any method called on XMLCollection is forwarded to XMLObject
$xml->customMethod();

Countable Interface

count()

Count the number of items in the collection.
public function count(): int
return
int
Returns the number of items.
$itemCount = count($xml);
// or
$itemCount = $xml->count();

IteratorAggregate Interface

getIterator()

Get an iterator for the items.
public function getIterator(): ArrayIterator
return
ArrayIterator
Returns an ArrayIterator instance.
foreach ($xml as $key => $value) {
    // Iterate through items
}

Complete Example

use Flowgistics\XML\Facades\XML;
use App\Transformers\UserTransformer;
use App\Models\User;

// Import and process XML
$xml = XML::import('users.xml')
    ->optimize(XML::OPTIMIZE_UNDERSCORE)
    ->transform('users')
    ->with(UserTransformer::class)
    ->cast('created_at')
    ->to('datetime');

// Access data
$users = $xml->users;

// Convert to array
$array = $xml->toArray();

// Convert to JSON
$json = $xml->toJson(JSON_PRETTY_PRINT);

// Iterate
foreach ($xml as $key => $value) {
    echo "$key: $value";
}

// Count items
echo count($xml);

Build docs developers (and LLMs) love