- How do I integrate XML Wrangler with an existing Saloon connector?
- XML Wrangler works natively with Saloon connectors. Use `XmlReader::fromResponse($this->response)` in your `resolve()` method to parse XML responses. For requests, pass XML strings via `withBody()` or use `XmlWriter` to generate structured XML. The package aligns with Saloon’s response handling, so no extra middleware is needed.
- Does XML Wrangler support large XML files (e.g., 50MB+) without memory issues?
- Yes, it supports streaming via `XmlReader::fromStream()`, which processes XML incrementally without loading the entire file into memory. This is ideal for batch processing or legacy system exports. For very large files, combine it with Saloon’s streaming responses for optimal performance.
- Can I use XML Wrangler with Laravel’s Eloquent or API Resources?
- Absolutely. XML Wrangler converts XML to PHP arrays/objects, which integrate seamlessly with Eloquent models and API Resources. For example, parse XML into an array and pass it to `Resource::collection()` or map it directly to Eloquent attributes. The package avoids forcing you into a new paradigm.
- What’s the best way to handle XML namespaces in complex APIs (e.g., SOAP)?
- Use `XmlReader::mapNamespaces()` to explicitly define namespace prefixes (e.g., `mapNamespaces(['ns' => 'urn:soap'])`). This ensures queries like `query('//ns:Order')` work correctly. Test namespace mappings early with sample XML to catch misconfigurations before production.
- Is XML Wrangler compatible with PHP 8.1 or earlier?
- No, it requires PHP 8.3+ due to its use of generics, nullable types, and strict typing. If you’re on PHP 8.1/8.2, consider upgrading or using a polyfill like `nikic/php-parser` for generics. For critical legacy projects, pair it with `SimpleXML` as a fallback in error handling.
- How do I validate XML against a schema (e.g., XSD) with XML Wrangler?
- XML Wrangler doesn’t include built-in XSD validation, but you can integrate `veezee/xml` for schema validation. Use it alongside `XmlReader` by validating the parsed XML before processing. For business logic, add custom assertions in your connector’s `resolve()` method to catch runtime errors.
- What’s the performance impact compared to raw SimpleXML or DOMDocument?
- XML Wrangler adds minimal overhead (~<5% latency) compared to `SimpleXML` or `DOMDocument` for typical use cases. Benchmark with your real-world XML payloads (e.g., 5MB+ files) to confirm. Optimize further by caching parsed responses or using `LazyQuery` for large datasets.
- Can I use XML Wrangler without Saloon for standalone XML parsing?
- Yes, but it’s designed as a Saloon plugin. For standalone use, focus on `XmlReader` and `XmlWriter` classes directly. If you’re not using Saloon, evaluate whether migrating to it (for HTTP clients) would simplify your XML workflow further.
- How do I test XML responses in Laravel/Pest or PHPUnit?
- Use `XmlWriter::toString()` to generate deterministic XML strings for snapshots in tests. For assertions, compare parsed XML arrays/objects with expected values. Pest/PhpUnit’s data providers work well for testing multiple XML structures. Document edge cases like CDATA or encoding in your test suite.
- What alternatives exist if XML Wrangler doesn’t fit my needs?
- For lightweight XML parsing, consider `SimpleXML` or `DOMDocument`. For schema validation, `veezee/xml` is a robust choice. If you’re using Saloon, `XML Wrangler` is the most integrated option, but for non-Saloon projects, `Sabre/XML` or `XMLParser` libraries offer broader features. Evaluate your need for streaming, namespaces, or Saloon integration.