- How do I integrate **xml-wrangler** with Saloon for SOAP API responses?
- Use the `XmlApiConnector` trait in your Saloon connector. Override the `resolve()` method to parse XML responses with `XmlReader::load($response->body())->query('//desiredNode')->map()`. The package handles namespaces and attributes automatically if configured in the reader.
- Does **xml-wrangler** support PHP 8.3, or is PHP 8.4+ required for generics?
- The package works on PHP 8.3+, but generics (e.g., `Query::fromArray<ProductDto>`) require PHP 8.4+. If you’re on 8.3, you’ll lose type safety for mapped results but can still use the core parsing and traversal methods without issues.
- Can I safely parse large XML files (e.g., 100MB+) without memory issues?
- Yes, use the `each()` method for lazy evaluation or stream the XML with `XmlReader::load($stream)->rewindable()`. Avoid `all()` for large files, as it loads everything into memory. The package is designed to handle streaming with minimal overhead.
- How do I handle XML namespaces, especially for SOAP APIs?
- Pass the namespace prefixes and URIs to `XmlReader::withNamespaces(['soap' => 'http://example.com/soap'])` before parsing. For SOAP, chain this with `query('//soap:Envelope/soap:Body')` to target namespaced nodes. The package validates namespace declarations during parsing.
- Is there a way to convert XML to Eloquent models or Laravel Collections?
- Yes, use `XmlReader::load()->map()` to convert XML nodes to arrays, then hydrate Eloquent models with `Model::hydrate()` or wrap in a `Collection` via `collect($reader->query()->map()->all())`. For bulk operations, chain `each()` with a closure to process nodes incrementally.
- What’s the performance difference between **xml-wrangler** and SimpleXML/DOMDocument?
- For small to medium XML files, **xml-wrangler** is comparable to SimpleXML but offers a more fluent API. DOMDocument is slower for parsing but more feature-rich for complex transformations. Benchmark your use case—**xml-wrangler** excels in Saloon integrations and type safety.
- How do I test XML parsing logic in Laravel/Pest or PHPUnit?
- Use `XmlReader::load($xmlString)->query()->map()` in tests, then assert results with `assertEquals()` or Pest’s `expect()`. For snapshot testing, serialize the parsed structure with `json_encode($reader->toArray())` and compare against a baseline file.
- Can I generate XML dynamically from Laravel Collections or Eloquent models?
- Yes, use `XmlGenerator::create()->root('response')->node('items', $collection->map(fn($item) => $item->toArray()))`. The generator supports nested structures, attributes, and CDATA. For Eloquent, chain `->node('user', $user->toArray())` directly on the generator.
- What alternatives should I consider if **xml-wrangler** lacks a specific feature?
- For XPath-heavy workflows, consider **Extenso/XML** or **XMLReader** (PHP’s built-in extension). If you need DOM manipulation, **DOMDocument** is more powerful but verbose. For Saloon users, **xml-wrangler** is the most integrated option—evaluate if its abstractions justify the trade-offs.
- How do I debug XML parsing errors or malformed responses?
- Use `XmlReader::load()->toXmlString()` to inspect the parsed structure or `->debug()` to log raw XML. For XPath issues, validate queries with `->query('//node')->first()` and check if the node exists. The package throws descriptive exceptions for malformed XML or missing nodes.