XML to PHP Class Generator

Classes with a fromXml() method built on SimpleXML

XML Input

PHP Output

Paste your XML on the left and the PHP code shows up here.

Or drop a file on this box, , or .

What the generator writes

From an XML sample the page builds one PHP class per distinct element, all in a single file that opens with declare(strict_types=1);. Every class has a static fromXml(\SimpleXMLElement $xml) that fills the object from a SimpleXML element, backed by a small XmlModelHelpers class for child, attribute and number lookups. Load a file with simplexml_load_file(), pass the root to Order::fromXml(), and you have typed objects.

With Typed properties on, fields use typed properties such as public ?int $qty = null;. Repeated elements become array with a /** @var Item[] */ docblock, because PHP has no typed arrays. Numbers and booleans are converted from text, and missing values stay null. The file declares strict_types=1 and the namespace you choose.

Namespaced documents work too: each lookup passes the element's namespace URI to children() and attributes(). We ran the output on PHP 8.3 against the page sample. Typed properties need PHP 7.4 or newer, and the fn arrow functions used for lists need 7.4 too. The XML you paste is processed in your browser, not uploaded.

How to use it

Paste, set the namespace, copy.

1

Paste a sample

Paste the document into XML Input, or click Upload for a file on disk. Because the sample order has two <item> elements, $item is declared as an array with an @var Item[] docblock and filled with array_map. Sample loads an order:

<order id="ORD-7731" status="paid">
  <item sku="SKU-101"><qty>2</qty><price currency="USD">24.99</price></item>
  <item sku="SKU-205"><qty>1</qty><price currency="USD">39.50</price></item>
</order>

If SimpleXML would choke on it, so does this page: the PHP pane shows the parser message. The XML Validator tells you which line to fix.

2

Set the namespace

Root class sets the outer class name; blank keeps Order. Namespace defaults to App\Models and accepts deeper ones such as App\Models\Import; clear it for none. Untick Typed properties on an older PHP and each property gets a docblock like /** @var int|null */ instead of a ?int type.

3

Copy or download

Download saves a .php file with all classes and a small XmlModelHelpers class they share. Copy puts it on the clipboard.

Where it helps

Payment and shipping APIs

Plenty of carrier and payment gateways still answer in XML. Map the response into objects once instead of chaining SimpleXML properties in every controller.

Laravel and Symfony imports

Parse product or stock feeds into typed objects in a job, then persist them with Eloquent or Doctrine.

WordPress and CMS exports

Migration scripts that read WXR or other XML exports get a clean data model to work with.

Testing

Load fixture files into objects and assert on properties instead of string-matching XML.

Common questions

Does fromXml need any extension?

Only SimpleXML, which is enabled in almost every PHP build. No Composer packages are required.

Why are lists typed as array?

PHP has no generic or typed arrays, so Item[] is not valid syntax. The generator writes public array $item = []; with a /** @var Item[] */ docblock that PhpStorm, PHPStan and Psalm understand.

How are attributes and text mapped?

Attributes are read through XmlModelHelpers::attr() and sit beside the child properties. An element with an attribute and text, such as <unitPrice currency="GBP">9.95</unitPrice>, becomes a UnitPrice class with ?string $currency and ?float $value, the text cast through toFloat().

What does the Namespace option change?

Only the PHP namespace line at the top. XML namespaces are handled separately and automatically in the lookups.

Which PHP version is required?

7.4 or newer, for typed properties and arrow functions. The file uses declare(strict_types=1);, see strict typing in the manual.

Is the XML uploaded?

No. The classes are built by script in your browser, so the XML is never posted to a server. Input over 256 KB is turned down; an order with two items is a big enough sample.

Related tools

Other converters you might reach for next: