Back to results for “JSON Handling in PHP”
PHP › XML Handling in PHP

In a modern PHP application that primarily uses JSON for its APIs, when might you still legitimately need to work with XML, and what strategies help cleanly integrate XML handling into an otherwise JSON centric codebase?

Advanced

Answer

While JSON has become the dominant data format for most modern web APIs due to its simplicity and lighter weight compared to XML, many real world integration scenarios still genuinely require working with XML, such as consuming data from an older legacy enterprise system, integrating with certain payment processors or shipping carriers that still rely on XML based protocols, or generating specific document formats like an RSS feed or a sitemap that are inherently XML based by design, and a clean strategy for handling this within an otherwise JSON centric application is to isolate all XML specific parsing and generation logic behind a dedicated adapter class, converting to and from plain PHP arrays or objects at that boundary so the rest of the application never needs to directly deal with XML specific concerns.
class LegacyShippingXmlAdapter {
    public function parseTrackingResponse(string $xml): array {
        $data = simplexml_load_string($xml);
        return ['status' => (string) $data->status, 'eta' => (string) $data->eta];
    }
}
Real-world example A modern e commerce application built around a JSON API integrates with an older third party shipping carrier that only supports XML, isolating all of that specific XML parsing logic within one dedicated adapter class so the rest of the application only ever works with plain, familiar PHP arrays.

Common follow-ups: What are some other real world systems or protocols that still commonly rely on XML today?;How does this adapter pattern approach compare to more broadly converting the entire application to work with a single unified internal data format?