XML to Ruby Converter - Generate Ruby Classes from XML Online
Convert XML data to Ruby classes with attributes, methods, and JSON serialization from XML documents. Free online tool.
XML to Ruby converter, Ruby classes, Ruby code generation, XML parsing, Nokogiri
How to Convert XML to Ruby Classes - Step by Step Guide
Input Your XML Schema
Start by adding your XML data that needs to be converted to Ruby classes. The tool analyzes the structure to generate appropriate Ruby classes with attributes and methods.
Example: Try This XML Data
Copy and paste this XML example to see how it works:
<?xml version="1.0" encoding="UTF-8"?> <blog-post id="POST-2024-001" published="true"> <title>Ruby on Rails Best Practices</title> <author>Rosalie Winters</author> <category>Web Development</category> <publish-date>2024-01-15</publish-date> <content> <summary>Learn the essential Ruby on Rails patterns</summary> <word-count>1250</word-count> <reading-time>5</reading-time> </content> <metadata> <views>2847</views> <likes>156</likes> <featured>true</featured> </metadata> </blog-post>
Review What Gets Generated
There are no settings to pick. About half a second after you stop typing, every element with children or attributes turns into one of these Ruby classes:
Get Generated Ruby Code
Watch the transformation! Your XML structure becomes clean, idiomatic Ruby classes.
Ruby Code Output
Your XML becomes these Ruby classes:
require 'nokogiri' require 'json' class Root attr_accessor :id, :published, :title, :author, :category def initialize(data = {}) data.each do |key, value| instance_variable_set("@#{key}", value) if respond_to?("#{key}=") end end def to_hash hash = {} hash[:id] = @id if defined?(@id) hash[:published] = @published if defined?(@published) hash end def to_json(*args) to_hash.to_json(*args) end end
Copy or Download Ruby Code
Perfect! Now you can use your Ruby classes in your applications for XML processing.
What is XML to Ruby Conversion?
XML to Ruby conversion transforms XML schemas into Ruby classes with attributes, methods, and JSON serialization capabilities. This process analyzes XML structure and generates Ruby classes that can parse and serialize XML data, making it easy to work with XML in Ruby applications and web services.
The generated Ruby classes include attr_accessor methods, initialize constructors, to_hash and to_json methods for serialization, and integration with Nokogiri for XML parsing, enabling seamless XML processing in Ruby applications.
Frequently Asked Questions
What goes into the generated Ruby file?
Each element that has child elements or attributes becomes a class with attr_accessor fields, an initialize that takes a hash, to_hash and to_json, and a from_xml class method. A small XmlParsing module at the top holds the helpers those methods share. Simple text elements such as <title> become fields on their parent rather than classes of their own.
Can I use the classes in a Rails or Sinatra app?
Yes. They are plain Ruby objects with no Rails dependency, so they work in a Rails service object, a Sinatra route or a one-off script. You need the nokogiri gem in your Gemfile (most Rails apps already have it). They are not ActiveRecord models, so if you want to store the data you still map it onto your own models.
How are repeated and nested elements handled?
Every copy of an element is looked at, not just the first one, so a field that only shows up in the second <item> still makes it into the class. An element that repeats under one parent becomes an array, nested elements get their own class, and an element with both attributes and text keeps the attributes next to a value field.
How does from_xml read my document, and do numbers stay exact?
Call Root.from_xml(File.read('data.xml')) or pass a Nokogiri node. It parses in strict mode, finds children by name and namespace, and converts values as it goes: whole numbers to Integer, decimals to Float, true and false to booleans. Anything else stays a String, including codes with leading zeros like 00501 and hex like 0x1F. Ruby Integers have no size limit, so a 19-digit ID such as 8901240544102066246 keeps every digit; decimals become Float, so 14.50 reads back as 14.5.
What naming rules does the generated code follow?
Classes use PascalCase and accessors use snake_case, so <publish-date> becomes publish_date and <blog-post> becomes BlogPost. Names that would break Ruby are adjusted: a keyword like end becomes end_, a name starting with a digit gets a field_ prefix, an element called <string> becomes StringElement so it never reopens Ruby's own String class, and two names that end up the same get a _2 suffix. The top-level element is always the Root class.
Is it free, and is my XML uploaded anywhere?
It is free and there is no sign-up. The conversion runs in your browser, so the XML you paste or upload is not sent to our server. Very large files can make the editor sluggish, because all the work happens on your own machine.