XML to Ruby Converter

Transform XML data into Ruby code with arrays, hashes, and Struct objects

XML Input

Ruby Output

About XML to Ruby Converter

Convert XML data to Ruby code with arrays, hashes, and Struct objects. Perfect for Rails seed data, testing fixtures, and Ruby scripts.

Key Features

  • Array of Hashes: Most common Ruby data structure with named keys
  • Array of Arrays: Simple nested arrays for basic data
  • Struct Objects: Type-safe objects with named attributes
  • Symbol or String Keys: Choose between :symbol or "string" keys for hashes
  • Type Detection: Automatic detection of strings, numbers, booleans, and nil
  • String Escaping: Proper escaping of special characters
  • Usage Examples: Includes code examples for each format

How to Use

  1. Input XML Data: Paste your XML data or upload an .xml file
  2. Select Format: Choose between Array of Hashes, Array of Arrays, or Struct
  3. Configure Options: Set key type (symbols/strings) or header inclusion
  4. Review Output: The Ruby code generates automatically
  5. Copy or Download: Use the Copy or Download button to save your .rb file

Output Format Options

  • Array of Hashes: Best for Rails, ActiveRecord, and general Ruby use. Easy to access by key name.
  • Array of Arrays: Simplest format, good for CSV-like data and basic processing.
  • Struct Objects: Type-safe with method-like access. Great for domain objects and testing.

Supported XML Structures

The converter recognizes several common XML table patterns:

  • <table><row>...</row></table>: Standard table structure
  • <data><record>...</record></data>: Data records pattern
  • <records><record>...</record></records>: Records collection
  • Repeated Elements: Any root with repeated child elements
  • Attributes: Also supports attribute-based data

Example Conversion

XML Input:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <record>
    <Name>John Doe</Name>
    <Age>28</Age>
    <City>New York</City>
  </record>
  <record>
    <Name>Jane Smith</Name>
    <Age>34</Age>
    <City>London</City>
  </record>
</data>

Array of Hashes Output:

# Array of Hashes format
data = [
  {
    :name => "John Doe",
    :age => 28,
    :city => "New York"
  },
  {
    :name => "Jane Smith",
    :age => 34,
    :city => "London"
  }
]

Struct Output:

# Struct format
Record = Struct.new(:name, :age, :city)

data = [
  Record.new("John Doe", 28, "New York"),
  Record.new("Jane Smith", 34, "London")
]

Common Use Cases

  • Rails Seeds: Generate seed data for db/seeds.rb
  • Testing Fixtures: Create test data for RSpec, Minitest
  • Factory Bot: Generate factory definitions
  • Data Migration: Convert XML data to Ruby for processing
  • Configuration: Create Ruby config files from XML
  • Scripts: Generate Ruby scripts for data processing

Type Detection

The converter automatically detects Ruby types:

  • Integers: Values like 42, -10 become Ruby integers
  • Floats: Values like 3.14, -0.5 become Ruby floats
  • Booleans: "true" and "false" become Ruby true/false
  • Nil: Empty values become nil
  • Strings: All other values become quoted strings

Key Name Sanitization

XML tags are automatically converted to valid Ruby identifiers:

  • Converts to lowercase with underscores
  • Removes special characters
  • Ensures names start with letters or underscores
  • Perfect for Ruby method names and hash keys

Symbol vs String Keys

Use Symbols when:

  • Working with Rails and ActiveRecord
  • You want better performance (symbols are immutable)
  • Following Ruby conventions

Use Strings when:

  • Keys might be dynamic or user-generated
  • Interfacing with JSON APIs
  • You need to modify keys at runtime

Privacy & Security

All conversions happen locally in your browser. Your XML data is never uploaded to any server, ensuring complete privacy and security.