FormatterHQ

XML to PHP Class Generator

Generate typed PHP classes from XML: attributes and child elements become typed properties, with constructors, getters and validation methods.

Input XML
Output PHP
Ready.

How to use the XML to PHP

  1. Paste your XML document into the Input editor, or use Sample, Upload file or Load URL, or press Sample to see an example.
  2. Leave Top-level class name empty to name the root type after the root element, or type your own name.
  3. The Types only option does not change PHP output; classes always include conversion and validation methods.
  4. Press Generate (it also updates as you edit), then use Copy or Download.

XML to PHP classes

PHP reads XML easily with SimpleXML or DOMDocument, but both return loosely typed objects where every value is a string. This XML to PHP generator writes classes with typed properties from a sample document, so your application can convert an XML response once and then rely on int, bool and nested objects.

Example: an API response

<response status="ok">
  <user id="17" admin="false">
    <email>ada@example.com</email>
    <zip>01234</zip>
  </user>
</response>
class Response {
    private User $user;     // json:user Required
    private string $status; // json:status Required
    // constructor, from(), to(), validate(), getters ...
}

class User {
    private string $email; // json:email Required
    private int $zip;      // json:zip Required
    private int $id;       // json:id Required
    private bool $admin;   // json:admin Required
}

The ZIP code problem

Look at $zip: the value 01234 looked like a number, so it was typed int, and as an integer it would be 1234. Postal codes, phone numbers, account numbers and ISBNs are identifiers, not quantities; change their types to string before using the class. This is the most common adjustment after generating classes from XML.

Filling the classes from SimpleXML

$xml = simplexml_load_string($body);
$u = $xml->user;
$user = new User(
    (string) $u->email,
    (string) $u->zip,          // after changing $zip to string
    (int) $u['id'],            // attributes via array access
    ((string) $u['admin']) === 'true',
);

SimpleXML exposes attributes with array syntax and child elements as properties, and every value must be cast. Note that (bool) 'false' is true in PHP, so compare the string instead. A popular shortcut, json_decode(json_encode($xml)), puts attributes under an @attributes key, which differs from the shape the generated from() methods expect; either adjust the keys or construct objects directly as above. When loading XML from untrusted sources, do not pass the LIBXML_NOENT flag, which enables entity substitution.

DOMDocument for complex documents

For documents with namespaces or when you need XPath, DOMDocument with DOMXPath gives more control than SimpleXML. The generated classes work the same way: query the values, cast them, and pass them to the constructors.

How the XML is mapped

Attributes and child elements become properties of the same class, element text beside attributes becomes $value, repeated elements become arrays, and namespace prefixes are removed. The root element name (response) becomes the class name Response.

Format the generated code with the PHP Formatter. For JSON APIs use JSON to PHP. Generation runs in your browser.

Frequently asked questions

Why is my ZIP code an int?
Numeric-looking text is typed as a number, which drops leading zeros. Change identifiers such as ZIP codes and phone numbers to string.
How do I read XML attributes with SimpleXML?
Use array access on the element, for example $user['id'], and cast the result to the type you need.
Can I pass SimpleXML output to the generated from() method?
Not directly. from() expects a stdClass shaped like the tool’s JSON view, while json_encode on SimpleXML nests attributes under @attributes. Construct the objects yourself or remap the keys.
Why is (bool) "false" true in PHP?
Any non-empty string except "0" casts to true. Compare the attribute text with "true" instead of casting.
How do I read namespaced XML with SimpleXML?
Elements in a namespace are not visible as plain properties. Call $xml->children("http://namespace-uri") to reach them, or register a prefix with registerXPathNamespace and query with xpath(). Then cast the values and pass them to the generated constructors as usual.

Last updated October 3, 2026

Related tools

JSON to PHP Generate PHP classes with typed properties from a JSON sample instantly, with nested types detected automatically. XML to Pike Generate Pike classes from a XML sample instantly, with nested types detected automatically. XML to Python Generate Python dataclasses with type hints from a XML sample instantly, with nested types detected automatically. XML to Ruby Generate Ruby Dry::Struct classes from a XML sample instantly, with nested types detected automatically. XML to Rust Generate Rust structs with Serde derives from a XML sample instantly, with nested types detected automatically. XML to Scala Generate Scala case classes from a XML sample instantly, with nested types detected automatically. XML to Swift Generate Swift Codable structs from a XML sample instantly, with nested types detected automatically. XML to TypeScript Generate TypeScript interfaces from a XML sample instantly, with nested types detected automatically. GraphQL to TypeScript Generate TypeScript interfaces, enums and union types from a GraphQL schema (SDL), with nullability and argument types. JSON Schema to TypeScript Turn JSON Schema into TypeScript interfaces and types with optional properties, enums, $ref definitions and JSDoc comments. JSON to C++ Generate C++ classes with nlohmann::json helpers from a JSON sample instantly, with nested types detected automatically. JSON to Crystal Generate Crystal classes with JSON mapping from a JSON sample instantly, with nested types detected automatically.