Ruby to XML Converter
Paste Ruby classes or objects. Get clean XML back.
What this tool does
Rails 5 moved model to_xml out into the separate activemodel-serializers-xml gem, yet Ruby apps still get asked for XML: a payment gateway with a SOAP API, a supplier that ingests product files, a Nokogiri Builder template someone has to keep in sync with a PORO. Paste the classes and the lines that set their attributes, press Convert, and our backend gives you XML with the class name as the root and an element for every attr_accessor you assigned.
Ruby values are mapped by what they are, not by how they print. BigDecimal("49.99") is written as 49.99 rather than the 0.4999e2 you see from inspect. Time, Date and DateTime become ISO-8601 strings (the format Time#iso8601 in the Ruby docs produces), a symbol such as :pending is written as pending, and nil leaves an empty element in place. Assign an array to order.items and the XML nests every object from that array under a single <items> parent.
Names come from your code: the root takes the class name as written (Order), and children use the attribute names, so order_id stays <order_id>. A Struct.new(:sku, :qty) is treated like a class with those accessors. A hash either becomes direct child elements, when its keys look like field names, or, for lookup tables, a series of Entry elements that each carry a key and a value. That is a little different from what a Rails to_xml call would give you (dasherized tags, type attributes), so if you are matching old Rails output, compare against a REXML dump of the real thing.
How to use it
Three steps. Same flow whether you paste a single Struct or a whole lib folder.
Paste your Ruby (or try the sample)
Paste the code into the left editor, require lines included. Load Sample loads three plain classes built on attr_accessor, then an Order filled in line by line with two OrderItem objects priced as BigDecimal and a London address.
Leave the require lines, module nesting and frozen_string_literal magic comment in place; none of them changes the output. Only classes, their accessors and the values you assign matter.
Hit Convert
Hit the green Convert button. The Ruby is posted to our backend, which follows the assignments to work out each attribute's value (nothing is evaluated, so a value coming from ENV or a method call cannot be resolved) and returns the XML. You will see a "Converting..." overlay while it works.
Copy the XML
Once the right pane fills in, save the result under spec/fixtures for an RSpec example, or parse it with Nokogiri::XML(xml) { |c| c.strict } to make sure it loads cleanly before you send it anywhere.
When this actually comes in handy
Building Nokogiri or Builder fixtures
You have a Ruby class and need an XML template for Nokogiri::XML::Builder or an ERB template. Paste the class, take the XML, paste it into your Builder block: done.
Rails to_xml seed data
An ActiveRecord model or PORO with 20 attributes becomes a ready-to-edit XML seed file for your integration tests or an external system that still consumes XML.
Legacy SOAP integrations
A Ruby client talking to an old SOAP/XML-RPC service needs a request body that matches a Ruby class. Paste the model, get the XML envelope shape, stop fighting Savon templates.
Keeping docs and code in sync
Generate XML response examples for a README, API reference, or XSD-backed documentation straight from the actual Ruby models, so the docs never drift from the code.
Common questions
Can I paste several classes at once?
Yes, as long as the paste stays under the 64 KB the backend accepts. If the code builds an instance, that object is serialized and the other classes appear where it references them. If you paste only class definitions, you get a template with empty text, zeros and false in place of values, and several unrelated classes are grouped under one <Root> element.
Does Struct.new shorthand work?
Yes. Order = Struct.new(:order_id, :customer_name, :total_amount) is treated the same as a class with matching attr_accessors. Keyword-style structs (Struct.new(:a, keyword_init: true)) work too: the resulting XML is identical either way.
How are BigDecimal, Time, Symbol, and nil handled?
BigDecimal("49.99") is written as 49.99, never in the 0.4999e2 form or with a type marker. Time, DateTime and Date are formatted as ISO-8601. A symbol like :express loses its colon. An attribute holding nil is still present as an empty tag, which keeps every record the same shape for whatever reads the XML next.
What about arrays, hashes, and nested objects?
An array gets a wrapper named after the attribute, so order.items = [item1, item2] produces <items> with an <OrderItem> for each element; an array of plain strings uses <Item> tags instead. Hashes that hold named fields nest as ordinary child elements, while lookup-style hashes become <Entry> pairs with a key and a value. An object assigned to an attribute, like order.shipping_address = address, is written out in full inside the parent.
Is my code stored?
Your Ruby is sent to our conversion backend the moment you click Convert, and the converted document is returned directly to this page without being stored. Look for Rails.application.credentials values, Stripe keys or real customer records pasted as literals and replace them first.
What if the code uses metaprogramming or define_method?
Nothing is executed, so attributes that only appear at runtime, through define_method, method_missing or an included hook, are invisible to the converter. It works from attr_accessor, attr_reader, Struct members and the assignments in your paste. When a field is generated dynamically, add an explicit accessor to the snippet you paste so it shows up.
Other tools you may need
Ruby objects that need to become XML often need to become JSON too, and XML you generate is worth a quick check. These help: