XML Input

Ruby Output

What the generator writes

Paste an XML sample and you get one Ruby class per element name. Each class has attr_accessor for every attribute and child element, an initialize that takes keyword arguments, and a self.from_xml(el) class method that builds the object from a parsed element.

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 conversion runs in your browser, so the XML you paste 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>

Parse errors are shown in the output panel. The XML Formatter helps when the input is one long line.

2

Name the root class and module

Root class renames the top-level class (empty uses the root element name). Module wraps everything in a namespace such as Billing or Billing::Import, 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 become accessors just like child elements. If an element has attributes and text, as in <price currency="USD">24.99</price>, the text goes into a value accessor.

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?

Names that would clash with Ruby keywords or core methods (class, hash, send) get 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 page parses it in your browser. Inputs over 256 KB are rejected; a few representative records are enough.

Related tools

Other converters you might reach for next: