Methods to parse XML in Ruby
There are many ways to parse XML. Here are some of the common ways :
- Using REXML
- Using Nokogiri
- Using Ox Gem
1. Using REXML
REXML is a standard XML parsing library that is included in Ruby’s standard library and REXML is a pure XML parser. It provides an interface to work with XML documents. Here’s an example of how to use REXML to parse:
require 'rexml/document'
xml_data = File.read('data.xml')
doc = REXML::Document.new(xml_data)
puts doc.elements['book/title'].text
doc.elements.each('book') do |book|
puts book.elements['title'].text
puts book.elements['author'].text
puts book.elements['year'].text
end
Output
Programming Ruby
Programming Ruby
David Thomas
2020
2. Using Nokogiri
Nokogiri is basically a XML library and also HTML parsing library for Ruby. It is best known for its speed and its flexibility. It contains robust features and provides ease in parsing and manipulating XML documents. Here’s an example how to use Nokogiri to parse:
require 'nokogiri'
xml_data = File.read('data.xml')
doc = Nokogiri::XML(xml_data)
puts doc.at_xpath('//book/title').text
doc.xpath('//book').each do |book|
puts book.at_xpath('title').text
puts book.at_xpath('author').text
puts book.at_xpath('year').text
end
Output
Programming Ruby
Programming Ruby
David Thomas
2020
3. Using Ox Gem
Ox is a fast XML parser and it is also an object serializer for Ruby. Its is mainly used to handle large XML files without loading the entire document into memory. Here’s an example how to use Ox Gem:
require 'ox'
File.open('data.xml') do |file|
Ox.sax_parse(MyHandler.new, file)
end
class MyHandler
def start_element(name)
puts "Start Element: #{name}"
end
def end_element(name)
puts "End Element: #{name}"
end
def text(value)
puts "Text: #{value.strip}" unless value.strip.empty?
end
end
Output
Start Element: book
Start Element: title
Text: Programming Ruby
End Element: title
Start Element: author
Text: David Thomas
End Element: author
Start Element: year
Text: 2020
End Element: year
End Element: book
Each approach and method has its own advantage of use and its based on scenarios and based upon factors related to document file size and performance requirements and complexity of usage.
How to parse XML in Ruby?
XML – Extensible Markup Language is used format on the platform for exchanging data and storing data on the web. Ruby, consists of a huge number of libraries and its syntax is flexible does provides several methods for parsing XML documents easily. In this article, we will look into various methods to parse XML in Ruby with its various libraries and techniques.