CalcCafe

XML to Ruby

Paste XML and instantly get quicktype-style Ruby Struct definitions with inferred field types.

Example

Input XML:

<book id="1">
 <title>Ruby Basics</title>
 <price>29.99</price>
 <inStock>true</inStock>
</book>

Generated Ruby:

Root = Struct.new(
 :id,    # String
 :title,   # String
 :price,   # Float
 :in_stock, # Boolean
 keyword_init: true
)

How it works

The tool parses XML with the browser's DOMParser, walks the element tree to infer a type model (scalars, nested objects, repeated elements as arrays), then emits Ruby Struct classes named from each tag with the root named Root.

Good to know

XML to Ruby turns a chunk of XML into ready-to-paste Ruby Struct definitions, with each field typed as String, Integer, Float, Boolean, a nested Struct, or an Array. It is aimed at Ruby and Rails developers who receive XML from a legacy API, a SOAP endpoint, a config file, or a data feed and want a typed scaffold to model that payload instead of hand-writing classes from scratch.

Reach for it when you are parsing an unfamiliar XML response and want a quick map of its shape, when you are migrating XML data into Ruby objects, or when you just need a starting point for value objects before wiring in a real parser like Nokogiri or Ox. Because everything runs in the browser, you can safely paste internal or sensitive payloads without sending them to a server.

Read the output top to bottom: the deepest nested Structs are defined first and the outermost one is always named Root, so dependencies are declared before they are used. The inline comment after each field is the inferred type, not real code — Array<Tag> means a repeated element became an array of a singularized nested Struct, and @-prefixed source keys indicate fields that came from XML attributes rather than child elements.

One caveat to keep in mind: types are guessed from sample values, so a field that happens to hold only digits in your sample becomes Integer even if the real schema allows text, and an empty element defaults to String. Feed in a representative sample that exercises every variation, and treat the generated Structs as a typed skeleton to review and adjust rather than a guaranteed contract.

Frequently asked questions

How are XML attributes handled?
Each attribute becomes its own snake_case field on the generated Struct, with a comment showing the inferred scalar type (String, Integer, Float, or Boolean) based on its value.
What happens with repeated XML elements?
When a tag appears more than once under the same parent, it is treated as an array. The field comment shows Array, and a singularized nested Struct is generated for repeated object elements.
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.

People also ask

Does XML to Ruby use Struct or a plain Ruby class?
It generates Ruby Struct definitions created with Struct.new and keyword_init: true, so each generated type is initialized with keyword arguments rather than positional ones.
How does the tool name nested Ruby Structs?
Each Struct is named from its XML tag converted to CamelCase, with the top-level element always named Root. Repeated object elements are singularized (for example a repeated tag becomes a Tag Struct), and duplicate names are de-duplicated with a numeric suffix.
What does keyword_init: true mean in the generated code?
It tells Ruby's Struct to accept keyword arguments on initialization, so you create instances like Book.new(title: 'x', price: 1.0) instead of by position. This makes the objects more readable and order-independent.
Can it convert XML to a Ruby Hash instead of Structs?
No, this tool only outputs Struct definitions. To get a Hash you would parse the XML separately, for example with Nokogiri or the Hash.from_xml helper available in ActiveSupport.
How are XML namespaces and prefixed tags handled?
Tag and attribute names are sanitized by stripping non-alphanumeric characters, so a namespace prefix like ns:Book is folded into the generated field and class name rather than preserved as a separate namespace. Review the output if your XML relies heavily on namespaces.
Why is a numeric-looking field typed as String instead of Integer?
Type inference checks the actual value: integers match a whole-number pattern, floats need a decimal point, and true or false become Boolean. Anything else, including empty elements or values with leading zeros that do not match the patterns, falls back to String.
Is XML to Ruby free and does it work offline?
Yes, it is completely free with no sign-up or limits, and it runs entirely in your browser, so it continues to work offline once the page has loaded.
What is the difference between converting XML to Ruby and to YAML or JSON models?
The conversion target is just the output language and structure: this tool emits Ruby Structs, while sibling tools target other languages. The XML parsing and type inference are similar, but Ruby Structs give you lightweight typed value objects native to Ruby.

Related tools