XML to PHP Class Generator
Classes with a fromXml() method built on SimpleXML
XML Input
PHP Output
What the generator writes
Paste an XML sample and you get one PHP class per element name, plus a static fromXml(\SimpleXMLElement $xml) on each class that fills the object from a SimpleXML element. 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.
Paste a sample
Paste XML into XML Input or Upload a file. Repeated elements need to appear twice to become arrays. 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>Parse errors are shown in the output panel. The XML Validator points at the exact line.
Set the namespace
Root class renames the top-level class (empty uses the root element name). Namespace takes a PHP namespace such as App\Models\Import; clear it for none. Untick Typed properties for docblock-only properties if you are stuck on an older PHP.
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 become properties like child elements. If an element has attributes and text, such as <price currency="USD">24.99</price>, the text goes into a value property.
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. It is parsed in your browser. Inputs over 256 KB are rejected.
Related tools
Other converters you might reach for next: