novakurosevic/
laravel-xml-converter
g4t.io

Simple XML Converter Package for PHP

by Paul Redmond

The XML Converter Package for PHP by Novak Urošević provides a simple helper to convert XML data into JSON and PHP arrays.

README from novakurosevic/laravel-xml-converter Open on GitHub →

Laravel XML Converter Package

🧾 Introduction

Since I started working with Laravel, I always wanted a simple helper that could convert XML files to JSON.

I created the first version of this package to do exactly that. While the package isn't perfect, it can handle a wide range of XML inputs. If you find any issues, feel free to report them.

I should note that this package was created with the help of AI. In my case, I used ChatGPT, and it saved me many hours of coding.

❓ What does this package do?

This package provides a flexible and powerful way to convert XML strings to JSON or PHP arrays. It supports:

  • DTD and XSD validation
  • Namespace tagging
  • CDATA handling

🚀 Installation

Requirements

  • PHP >= 8.1
  • Laravel >= 9
  • PHP Extensions:
    • simplexml
    • libxml
    • dom

Composer Installation

composer require noki/laravel-xml-converter

⚙️ PHP Extensions Setup

Linux (Debian/Ubuntu)

sudo apt-get update
sudo apt-get install php-simplexml php-xml php-dom

Example for PHP 8.3 — be sure to use the correct package names:

First, check your PHP version:

php -v

Then install extensions for your version:

sudo apt-get install php8.3-simplexml php8.3-xml php8.3-dom

macOS (with Homebrew)

brew install php
# OR, if PHP is already installed:
brew reinstall php

Required extensions (simplexml, libxml, dom) are bundled with PHP on macOS.


Windows

  1. Open your php.ini file.
  2. Ensure the following lines are uncommented (remove the ; at the beginning):
extension=simplexml
extension=dom
extension=libxml
  1. Restart your web server (Apache, Nginx) or PHP-FPM.

📦 Usage

Namespace

use Noki\XmlConverter\Convert;

1. Convert XML to JSON

$json = Convert::xmlToJson($xmlString);

2. Convert XML to array

$array = Convert::xmlToArray($xmlString);

3. Full control with options

$array = Convert::xmlToArray(
    $xmlString,
    namespace_in_tag_name: true,
    is_cdata: true,
    schema_path: '/path/to/schema.xsd' // or '' to enable DTD validation
);
  • namespace_in_tag_name: If true, keys will include namespace prefixes (e.g., ns:tag).
  • is_cdata: If true, CDATA sections are preserved as-is.
  • schema_path:
    • Path to an .xsd file for XSD validation.
    • Use '' (empty string) to enable DTD validation (your XML must contain <!DOCTYPE ...>).
    • Use null to skip validation.

4. Convert XML to JSON with options

$json = Convert::xmlToJson(
    $xmlString,
    namespace_in_tag_name: true,
    is_cdata: false,
    schema_path: null
);

🧪 Example

Sample XML with namespaces and CDATA

$xml = <<<XML
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="book.xsd">
    <title><![CDATA[The Great Gatsby]]></title>
    <author>F. Scott Fitzgerald</author>
    <year>1925</year>
</book>
XML;

$json = Convert::xmlToJson($xml, true, true, '/path/to/book.xsd');

🛠 Error Handling

  • If the XML is invalid, an InvalidArgumentException will be thrown.
  • If schema validation fails, a descriptive exception is thrown (with optional logging).
  • CDATA sections and empty tags are handled gracefully.

🧩 Integration with Laravel

You can use this package in controllers, services, or jobs:

use Noki\XmlConverter\Convert;

$data = Convert::xmlToArray($request->getContent(), true);

📄 License

MIT License. See LICENSE.md for details.


🙌 Contributing

Feel free to fork and submit a PR! Bug reports and feature requests are always welcome.


🔗 Author


📝 Changelog

Please see CHANGELOG.md for recent updates.

More from the ecosystem

marcreichel/
laya-php
g4t.io
3 53

LayaPHP: Self-Hosted Text Classification for PHP and Laravel

Classify text in PHP without an LLM bill: typed decisions in 100+ languages, self-hosted. Laravel-ready SDK for Laya, a Jev AI alternative.

ai classification decision-engine
marcreichel/laya-php via Laravel News
Josh-Dovey/
postcodes-laravel
g4t.io
5 9

Postcodes for Laravel: GB Postcode Lookup and Geography Data

Postcodes for Laravel adds typed GB postcode lookups, validation, geography data, distance searches, and test fakes through the GB Postcodes API.

Josh-Dovey/postcodes-laravel via Laravel News
RedberryProducts/
mailbox-for-laravel
g4t.io
4 115

Mailbox for Laravel: Preview and Test Rendered Email

Mailbox for Laravel captures outgoing mail in a local dashboard and lets you test rendered HTML, recipients, and attachments with fluent assertions.

RedberryProducts/mailbox-for-laravel via Laravel News