Ruby REXML Tutorial 🎯

beginner
16 min

Ruby REXML Tutorial 🎯

Welcome to our deep dive into Ruby's REXML library! In this tutorial, we'll explore how to parse, create, and manipulate XML documents using Ruby's built-in REXML library. By the end, you'll be ready to tackle real-world XML challenges.

What is XML? 📝

XML (eXtensible Markup Language) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. XML is used to transport and store data, especially when that data is structured and needs to be shared between different systems.

What is REXML? 📝

REXML is a Ruby library for parsing and creating XML documents. It's built into Ruby since version 1.8, so you don't need to install anything extra to use it!

Parsing XML with REXML 🎯

To parse an XML document with REXML, you'll use the REXML::Document.new(xml_string) method. Let's try it with a simple example:

ruby
require 'rexml/document' xml_string = <<-XML <books> <book id="001"> <title>The Catcher in the Rye</title> <author>J.D. Salinger</author> </book> <book id="002"> <title>To Kill a Mockingbird</title> <author>Harper Lee</author> </book> </books> XML doc = REXML::Document.new(xml_string)

In this example, we define an XML string and then create a REXML::Document object with it. Now we can navigate through the XML and extract data using various methods.

Navigating the XML 🎯

To navigate the XML, you'll use methods like root, elements, and element to move around. Let's try an example:

ruby
books = doc.root # Get the root element books.elements.each('book') do |book| id = book.attributes['id'] title = book.elements['title'].text author = book.elements['author'].text puts "Book ID: #{id}, Title: #{title}, Author: #{author}" end

In this example, we navigate to the root element (books), then loop through each book element. We extract the id, title, and author by navigating to their respective elements and getting the text.

Creating XML with REXML 🎯

To create an XML document with REXML, you'll use the REXML::Element class. Let's try an example:

ruby
require 'rexml/document' book = REXML::Element.new('book') book.add_attribute('id', '001') title = REXML::Element.new('title') title.text = 'The Catcher in the Rye' author = REXML::Element.new('author') author.text = 'J.D. Salinger' book.add_element(title) book.add_element(author) doc = REXML::Document.new(book) puts doc.to_s

In this example, we create a book element and add an attribute to it. Then, we create title and author elements and add them to the book. Finally, we create a REXML::Document object with our book element as the root.

Quiz 🎯

Quick Quiz
Question 1 of 1

What method do you use to parse an XML document with REXML?

Wrapping Up ✅

Now you've learned how to parse and create XML documents using Ruby's REXML library. With this knowledge, you can work with XML data in your Ruby projects with confidence. Keep practicing, and you'll soon be able to tackle more complex XML scenarios!