XML to Ruby Class Generator

Plain Ruby classes with a from_xml method built on REXML

XML Input

Ruby Output

Paste your XML on the left and the Ruby code shows up here.

Or drop a file on this box, , or .

What the generator writes

Every distinct element in your XML becomes a plain Ruby class. Every attribute and child element gets an attr_accessor, initialize takes keyword arguments that default to nil, and self.from_xml(el) builds the object from a parsed element. The file ends with a usage line: Invoice.from_xml(REXML::Document.new(File.read('invoice.xml')).root).

The parsing code uses REXML, which ships with Ruby, so it runs without adding a gem. Numbers are converted with Integer() and Float(), true/false become booleans, repeated elements come back as arrays, and anything missing is nil. A small XmlModelHelpers module at the top holds the shared conversion code.

If you already use Nokogiri, the classes are still useful; only the from_xml bodies need adapting. The Ruby source is generated by the page itself, in your browser, so the invoice data is not uploaded.

How to use it

Paste a sample, optionally pick a module, and copy the file.

1

Paste a representative sample

Paste XML into XML Input or click Upload. Show repeated elements at least twice so they become arrays. Sample loads a short invoice:

<invoice number="INV-2041" currency="USD">
  <customer><name>Ava Chen</name></customer>
  <line sku="SKU-101"><quantity>2</quantity><unitPrice>24.99</unitPrice></line>
  <line sku="SKU-205"><quantity>1</quantity><unitPrice>39.50</unitPrice></line>
</invoice>

When the XML will not parse, the Ruby pane shows the parser message instead of classes. If the input is one long line, run it through the XML Formatter first so the error is easier to spot.

2

Name the root class and module

Root class picks the outer class name; blank turns <invoice> into Invoice. Module nests everything, helpers included, inside module Billing or, for Billing::Import, two nested modules, which keeps generic names like Line from clashing with the rest of your app.

3

Copy or download

Download saves a .rb file. The last line shows how to load a document: Invoice.from_xml(REXML::Document.new(File.read('invoice.xml')).root).

Where it helps

Importing partner files

A nightly job that reads supplier invoices or stock files gets typed objects instead of digging through elements with XPath strings everywhere.

Rails service objects

Drop the classes into app/models or lib, map them onto ActiveRecord models in one place, and keep the XML details out of controllers.

Scripts and one-off migrations

For a quick migration script, generated from_xml methods get you from file to Ruby objects in a couple of minutes.

Testing

Build fixtures from real sample files and assert on attributes rather than on raw XML strings.

Common questions

Do I need any gems?

No. The code only requires rexml/document. REXML ships with Ruby; on Ruby 3.0 and later it is a bundled gem that is installed by default.

How are attributes and text handled?

Attributes are read with el.attributes['code'] and get accessors like any child. An element holding an attribute and text, for example <fee code="LATE">15.00</fee>, becomes a Fee class with code and a value accessor converted through Float().

What types do the values get?

Whole numbers are parsed with Integer(value, 10), decimals with Float(), and true/false become booleans, based on the values in your sample. Numbers with a leading zero stay strings. Missing values are nil.

Why is a field called class_value or object_id_value?

If a tag name matches a Ruby keyword or a method every object already has (class, hash, send), it gets a _value suffix so the accessor does not override something Ruby relies on.

Can I serialize back to XML?

Not with the generated code. It reads XML only. Writing XML back is usually a short method per class with REXML or a builder.

Is the XML uploaded?

No. The classes are written by JavaScript in your browser tab, so the XML is not uploaded. Inputs above 256 KB are rejected; an invoice with two line items gives the generator all it needs.

Related tools

Other converters you might reach for next: