zf1/zend-xml
Zend Framework 1 XML component, split out as a standalone package. Provides XML handling utilities such as DOM/SimpleXML helpers and security-conscious parsing features used by legacy ZF1 apps that need dependable XML processing without the full framework.
Installation Add the package via Composer (if available in a modern fork or via legacy support):
composer require zf1/zend-xml
Note: Since this is a legacy Zend Framework 1 package, ensure your Laravel project is configured to support PHP 5.3+ (if needed) or use a maintained fork like zendframework/zend-xml (ZF2/ZF3).
Basic Usage Import the core class and parse XML:
use Zend_Xml_Element;
use Zend_Xml_Exception;
$xmlString = '<root><item>Test</item></root>';
$element = Zend_Xml::fromString($xmlString);
echo $element->item; // Output: "Test"
First Use Case: Parsing API Responses Fetch an XML API response (e.g., from a legacy system) and extract data:
$response = file_get_contents('https://legacy-api.example.com/data.xml');
$xml = Zend_Xml::fromString($response);
$title = $xml->response->title->__toString();
Transforming XML to Arrays/Objects Recursively convert XML to a Laravel-friendly structure:
function xmlToArray($element) {
$array = [];
foreach ($element->children() as $child) {
$array[$child->getName()] = $child->__toString();
}
return $array;
}
$data = xmlToArray($xml->root);
Generating XML from Eloquent Models Serialize a model to XML for external systems:
$user = User::find(1);
$xml = new Zend_Xml_Element('user');
$xml->addChild('id', $user->id);
$xml->addChild('name', $user->name);
return $xml->saveXml();
Integration with Laravel HTTP Clients Use with Guzzle or HTTP Client to handle XML APIs:
$response = Http::get('https://api.example.com/data.xml');
$xml = Zend_Xml::fromString($response->body());
$xml = Zend_Xml::fromString($xmlString, [], [], 'http://example.com/ns');
$xml = Zend_Xml::fromString($request->xml);
$validator = Validator::make($xml->toArray(), ['required' => 'string']);
$cacheKey = 'api_response_' . md5($url);
$xml = Cache::remember($cacheKey, now()->addHours(1), function() use ($url) {
return Zend_Xml::fromString(file_get_contents($url));
});
Deprecation Warnings
$this->app->singleton('zend_xml', function() {
return new Zend_Xml();
});
TypeError for undefined methods. Use a polyfill or fork.Namespace Collisions
Xml helpers:
use Zend_Xml as ZendXml; // Alias to prevent clashes
Memory Limits
memory_limit. Stream parsing with SimpleXML or DOMDocument as a fallback:
$dom = new DOMDocument();
$dom->loadXML($xmlString);
Zend_Xml to throw exceptions on errors:
Zend_Xml::setOptions(['exceptions' => true]);
saveXml() to debug parsed structures:
echo $xml->saveXml(); // Compare with original input
Zend_Xml_Element for domain-specific logic:
class CustomElement extends Zend_Xml_Element {
public function getFormattedValue() {
return strtoupper($this->__toString());
}
}
public function register() {
$this->app->bind('zend_xml', function() {
return new Zend_Xml();
});
}
public function handle($request, Closure $next) {
if ($request->isXml()) {
$request->merge(['xml_data' => Zend_Xml::fromString($request->getContent())]);
}
return $next($request);
}
How can I help you explore Laravel packages today?