PHP to XML Converter
Paste PHP classes or objects. Get clean XML back.
What this tool does
If you have ever had to hand-craft an XML payload for a PHP SoapClient call, a WordPress plugin export, or an old partner integration that still speaks XML, you know how much busywork that is. Paste the PHP here and you get back well-formed XML in one pass. A single constructor-promoted class, a full value object file, or a populated instance — same result: a complete XML document with every property preserved.
It is not blind string-replacement. The converter reads modern PHP 8+ properly: constructor-promoted properties behave the same as declared ones, readonly fields come through as regular elements, typed properties (string, int, float, bool) preserve their types, DateTimeInterface values render as ISO-8601 strings, null becomes an empty element (not dropped), and arrays turn into container elements with one child per entry — matching what SimpleXMLElement or DOMDocument would typically produce.
Class structure and metadata are honoured. Each top-level class becomes a root element named after the class, public properties become child elements in declaration order, nested value objects expand inline, and array properties become wrapper elements. PHP attribute hints like #[XmlRoot("x")] or docblock tags like @XmlElement("x") rename elements where you specify them. Paste a Laravel Eloquent model, a Symfony Serializer DTO, or a plain value object — the XML comes out the way a hand-written SoapVar or SOAP request body would.
How to use it
Three steps. Same flow whether you paste one class or the entire <code>src/Entity</code> folder.
Paste your PHP (or try the sample)
Drop your PHP into the left editor as-is. A constructor-promoted class, a readonly value object, multiple classes, or a populated instance — all fine. Click Load Sample to see a realistic Order example first.
You do not need to remove the <?php tag, strip namespaces, or delete docblocks. Keep the code as it looks in your IDE. Just paste.
Hit Convert
Click the green Convert button. The tool reads the PHP, keeps every class and property, and builds the XML in one pass. A short shimmer shows while it runs.
Copy the XML
The right panel fills with indented, well-formed XML that any standards-compliant parser will accept. Drop it into a SoapClient request, a fixture for PHPUnit, a WordPress export file, or an API docs example.
When this actually comes in handy
Building SoapClient request fixtures
You are talking to a legacy SOAP endpoint via <code>SoapClient</code> and need a request body that matches a PHP class. Paste the class, take the XML, wrap it with <code>SoapVar</code> or feed it to <code>__doRequest()</code>.
WordPress/Drupal XML exports
A custom post-type class or Drupal entity with 20 fields becomes a ready-to-edit WXR or XML export template — no more hand-crafting element structures to match the PHP shape.
Symfony/Laravel XML responses
Your API mostly returns JSON but one client still wants XML. Paste the DTO, get the XML envelope, drop it into a Symfony Response or Laravel ResponseFactory example and move on.
PHPUnit fixtures and snapshot tests
Turn value-object instances from your unit tests into XML fixture files for integration tests, contract tests, or mock SOAP servers — without hand-writing a matching XML template by eye.
Common questions
Does it handle constructor promotion and readonly properties?
Yes. Constructor-promoted parameters (public string $orderId inside __construct) become child elements just like declared properties. readonly is treated the same as a regular property for serialization — the XML is identical. PHP 8.2 readonly class also works.
Can I paste several classes at once?
Yes — paste a whole entity folder or a single file with multiple classes. Each top-level class comes through with nested types expanded and parent-class properties rolled in. Traits are treated as contributing their properties. Nothing gets silently dropped.
How are DateTime, null, float, and bool handled?
Instances of DateTimeInterface (DateTime, DateTimeImmutable) render as ISO-8601 strings. null becomes an empty element (<field/>) rather than being omitted, so the XML shape stays consistent. float uses plain decimal notation with no locale-specific separators, and bool serializes as true/false text — matching what SimpleXMLElement produces.
What about arrays and nested objects?
Array properties become container elements with one child per entry. An /** @var OrderItem[] */ public array $items turns into <items><OrderItem/><OrderItem/></items>. Associative arrays use their string keys as element names. Nested value objects expand inline — the same way DOMDocument would build the tree.
Is my code stored?
Your PHP is sent to the backend for conversion and is not persisted — we do not log the payload. As always with online tools, if the code contains genuinely sensitive literals (DB passwords, API keys), scrub or mock them before pasting.
What if the code uses magic methods or dynamic properties?
Dynamic properties (added at runtime via __set or stdClass casting) and magic getters (__get) cannot be inferred from static code alone. The tool reads declared properties, constructor-promoted parameters, and explicit assignments. If a field only exists through __get, declare it as a property so the converter can pick it up.
Other tools you may need
PHP to XML is one piece of the puzzle. These tools pair well with it: