- How do I install veewee/xml in a Laravel project?
- Run `composer require veewee/xml` in your project root. The package integrates with Laravel’s service container, so you can bind components (e.g., `XmlWriterInterface`) in `AppServiceProvider` or use facades for Blade/console access. No additional Laravel-specific setup is required for basic usage.
- Does veewee/xml work with Laravel’s Eloquent models for XML exports?
- Yes. Use the `XmlWriter` component to serialize Eloquent models to XML. For example, create a `toXml()` method on your model or use a response macro like `Response::macro('xml', fn($model) => Xml::writer()->write($model->toArray()))` for API responses.
- What’s the difference between v3 and v4 of veewee/xml?
- v3 supports PHP 8.1–8.3 and lacks PHP 8.4’s DOM spec compliance, while v4+ requires PHP 8.4+ for stricter, future-proof XML handling. v3 remains actively maintained for legacy Laravel apps (e.g., PHP 8.1 LTS). Check the [support table](https://github.com/veewee/xml#support-table) for your PHP version.
- Can I use veewee/xml to parse XML API requests in Laravel?
- Absolutely. The `XmlReader` component handles memory-safe streaming of large XML payloads. Register a middleware (e.g., `XmlRequestMiddleware`) to parse incoming XML requests into arrays or objects, then process them like JSON requests. Example: `Xml::reader()->read($request->getContent())`.
- How does veewee/xml handle XSD validation in Laravel?
- The `XSD` component validates XML against schemas. Integrate it with Laravel’s validator by creating a custom rule (e.g., `XmlSchemaValidator`) or use it in middleware to validate XML requests before processing. For responses, validate XML output before sending to ensure compliance with third-party schemas.
- Is veewee/xml compatible with Laravel’s caching system?
- Yes. Cache XML transformations (e.g., XSLT or XSD-validated outputs) using Laravel’s cache drivers. For example, cache the result of `Xml::xslt()->transform($xml)` with `Cache::remember('xml_transformed', now()->addHour(), fn() => ...)`. This avoids reprocessing large XML files repeatedly.
- What are the alternatives to veewee/xml for XML in Laravel?
- For simple XML parsing, `SimpleXMLElement` (PHP’s built-in extension) or `spatie/xml-to-array` (for converting XML to arrays) may suffice. For advanced use cases, consider `DOMDocument` with custom wrappers. However, veewee/xml stands out with its type-safe API, memory-safe streaming, and Laravel-specific integrations (e.g., facades, validation).
- How do I handle malformed XML errors in veewee/xml?
- The package includes robust error handling helpers (e.g., `XmlErrorHandler`). Wrap XML operations in try-catch blocks or use the `Xml::errorHandler()` to log or rethrow errors. For APIs, return structured error responses (e.g., `response()->json(['error' => 'Invalid XML'], 400)`) when parsing fails.
- Can veewee/xml replace SimpleXMLElement in Laravel?
- Yes, but incrementally. Start by replacing `SimpleXMLElement` for XML generation with `XmlWriter` (more type-safe and memory-efficient). For parsing, use `XmlReader` instead of `simplexml_load_string()`. Test performance and memory usage in your Laravel app before full migration, especially for large XML files.
- What’s the roadmap for XSLT/XSD in veewee/xml?
- The roadmap includes integrating Saxon/C for XSLT 3.0/XPath 3.1 and advanced schema validation, but this depends on PHP 8+ support from Saxon/C (track [issue #4842](https://saxonica.plan.io/issues/4842)). Current XSD/XSLT features use PHP’s built-in extensions. For now, use the existing components for basic transformations and validation.