extphp/xml-to-json

An XML to JSON converter that will properly preserve attributes.

Installs: 19 707

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/extphp/xml-to-json

v0.2.0 2019-08-21 08:29 UTC

This package is auto-updated.

Last update: 2025-09-21 22:33:27 UTC


README

Build Status Latest Stable Version License Total Downloads

An XML to JSON converter that will properly preserve attributes.

Installation

composer require extphp/xml-to-json

Usage

A generic usage, usefull when the SimpleXMLElement instance already exists.

use ExtPHP\XmlToJson\XmlToJsonConverter;

$string = '<node attr1="value1" attr2="value2"><child>child value</child></node>';
$xml = simplexml_load_string($string);

$converter = new XmlToJsonConverter($xml);
$converter->toArray();      // convert xml to array
$converter->toJson();       // convert xml to json

A quick approach when you need to convert a XML string to array or json.

use ExtPHP\XmlToJson\JsonableXML;

$xml = new JsonableXML('<node attr1="value1" attr2="value2"><child>child value</child></node>');
json_encode($xml);      // convert xml to json

// These methods are also available directly on the xml object.
$xml->toArray();        // convert xml to array
$xml->toJson();         // convert xml to json