XML to PHP Converter - Generate PHP Classes from XML Online
Generate typed PHP classes with getters, setters, toJson() and a SimpleXML loader from an XML document.
XML to PHP converter, PHP classes, PHP code generation, XML parsing, SimpleXML
How to Convert XML to PHP Classes - Step by Step Guide
Paste Your XML
Paste an XML document, upload an .xml file or click Sample. The classes are generated as you type, and every copy of a repeated element is looked at, not just the first one.
Example: Try This XML Data
Copy and paste this XML example to see how it works:
<?xml version="1.0" encoding="UTF-8"?> <e-commerce-order id="ORD-2024-001" status="confirmed"> <customer-info> <customer-id>CUST-12345</customer-id> <name>Maria Rodriguez</name> <email>[email protected]</email> <is-premium>true</is-premium> </customer-info> <order-details> <order-date>2024-01-15</order-date> <total-amount>299.99</total-amount> <currency>USD</currency> </order-details> </e-commerce-order>
Check What the Classes Include
There are no settings to pick. Every generated PHP class gets the same building blocks:
Review the Generated PHP Code
Each element with children or attributes becomes a class named after it, with property types worked out from the values in your XML.
PHP Code Output
The Step 1 example gives classes like these (shortened):
<?php
class Root
{
private ?string $id = null; // attribute id
private ?string $status = null; // attribute status
private ?CustomerInfo $customerInfo = null; // element <customer-info>
private ?OrderDetails $orderDetails = null; // element <order-details>
public function getId(): ?string
{
return $this->id;
}
// ...setters, toArray(), toJson(), fromXml() and fromElement()
}
class CustomerInfo
{
private ?string $customerId = null; // element <customer-id>
private ?string $name = null; // element <name>
private ?string $email = null; // element <email>
private ?bool $isPremium = null; // element <is-premium>
// ...
}
class OrderDetails
{
private ?string $orderDate = null; // element <order-date>
private ?float $totalAmount = null; // element <total-amount>
private ?string $currency = null; // element <currency>
// ...
}Copy or Download PHP Code
Copy the classes to your clipboard or download them as xml_classes.php and load your XML with Root::fromXml().
What is XML to PHP Conversion?
XML to PHP conversion reads the structure of an XML document and writes PHP classes that mirror it, so instead of digging through SimpleXMLElement objects and casting strings by hand you work with $order->getCustomerInfo()->getEmail() and get real ints, floats and bools back.
The generated file contains one class per element that has children or attributes, a few small helper functions for reading child elements, attributes and text, and a fromXml() method on every class. It reads XML into objects; it doesn't write XML back out.
Frequently Asked Questions
What PHP features are included in the generated classes?
Each element that has child elements or attributes becomes a PHP 8 class with typed private properties, a getter and a setter for each one, a constructor that takes an associative array, and toArray() and toJson(). Every class also gets fromXml() and fromElement(), which read a document with SimpleXML and fill the whole object tree. Elements that only hold text become typed properties on their parent class.
Can I use the generated classes in Laravel, Symfony or CodeIgniter?
They're plain PHP 8 classes with no dependencies, so you can drop them into any framework like any other class, for example as data objects for an XML feed or a SOAP response. They aren't Eloquent models and don't talk to a database. The file has no namespace line, so add one if your autoloader expects it.
How does the tool handle nested and repeated XML elements?
Every element with child elements or attributes gets its own class, and its parent holds an instance of it. An element that repeats under the same parent becomes an array of objects or values. All copies of a repeated element are looked at, so a field that only shows up in the third <item> still gets a property, and copies that lack it simply leave it null.
Can I load my XML into these classes with SimpleXML?
Yes. Root::fromXml($xmlString) parses the document with simplexml_load_string() and fills in attributes, child elements, namespaced elements and the text of elements that also carry attributes. If you already have a SimpleXMLElement, pass it to fromElement() instead. XML that isn't well-formed throws an InvalidArgumentException. The code needs the SimpleXML and DOM extensions, which a standard PHP 8 build has.
How are names and property types chosen?
Class names are PascalCase and properties camelCase, so <customer-info> becomes CustomerInfo and $customerInfo. Names PHP won't accept for a class, such as list, string, directory or exception, get an Element suffix, and names that would clash get a number. Types come from the values in your XML: whole numbers that fit in 64 bits are int (a 19-digit ID like 8901240544102066246 keeps every digit), decimals are float, only true and false become bool, and values such as 00501 or a mix of numbers and text stay string.
Is the XML to PHP converter free, and is my XML uploaded?
It's free and needs no sign-up. The conversion runs in your browser, so the XML you paste or upload never leaves your computer. Very large files are only limited by your browser's memory.