HTML and XML Parsers in Ruby
The Ruby developer community offers some great HTML and XML parsing libraries that can meet all your website data collection needs - there are many options available. When choosing which one to go with, you can consider the following criteria:
- It should be open source.
- There should be good community support.
- It should provide good performance.
- It should be actively maintained. It should be lightweight.
- There should be good documentation.
To help you choose the right parser for your project, this article will analyze six HTML and XML parsing libraries in Ruby based on the above criteria.
Nokogiri
Nokogiri is an open-source HTML and XML parsing library for Ruby. Internally, it uses parsers such as libxml2 and xerces to perform its core functions. In addition to providing an API that complies with a strict security policy, it also offers features such as a DOM parser, SAX parser, Push parser, and XSLT transformation.
Nokogiri is the standard HTML parser for Mechanize, a library for web browser automation.
Here is an example of using Nokogiri:
require 'nokogiri'
doc = "<html><head><title>page title</title></head><body>page content</body></html>" parsed_data = Nokogiri::HTML.parse(doc)
puts parsed_data.title => "page title"
Advantages
- It is the most popular and widely used Ruby library for HTML and XML parsing.
- You will find plenty of discussions on Q&A forums (e.g., Stack Overflow), and you can easily find blogs and tutorials online to help you get started.
- It is fast and efficient due to leveraging the power of native parsers libxml2 and xerces.
- It has frequent releases that address bug issues and introduce updates and improvements to the library.
- It has a powerful API that easily handles poorly formatted HTML to reduce instability.
- It searches for content in HTML and XML documents using XPath and CSS selectors.
- It has comprehensive documentation, so you won't have trouble understanding specific functionality.
Disadvantages
- The main limitation of Nokogiri is that it alone is insufficient for parsing a document whose content is loaded on the page using Ajax.
- You may also encounter difficulties parsing a document if the website requires authentication, such as a username and password.

Hpricot
Hpricot is another open-source HTML parsing library for Ruby. It was originally designed for parsing HTML documents, although technically it is also capable of parsing XML documents. It accurately finds elements in HTML documents, so if you see any content in an HTML document in the browser, Hpricot will definitely parse it.
Here is an example of using Hpricot:
require 'hpricot'
doc = "<html><head><title>page title</title></head><body>page content</body></html>" parsed_data = Hpricot(doc).at("title").inner_html
puts parsed_data => "page title"
Advantages
- It is very fast because most of the performance-related functionality is written in C.
- It only requires Ruby to be installed - no other dependencies, making it very lightweight.
- It has a powerful API that allows reading and parsing poorly formatted HTML easily, like Nokogiri.
- It simplifies finding HTML elements using XPath and CSS selectors.
- It has good documentation to help you navigate the library.
Disadvantages
- It is no longer maintained, and it lacks strong community support, so you will likely run into issues when working with this library.
Technically, Hpricot is not designed for parsing XML. It does not validate XML documents, so you may encounter issues when working with malformed XML.
Ox (Optimized XML)
Optimized XML (Ox), as its name implies, is a fast and efficient XML parsing library for Ruby with HTML processing capabilities. It is open source and provides a clean and simple API. Ox processes XML documents in three ways: as a general XML parser and writer, as a fast object/XML marshaler, and as a streaming SAX parser. It aims to replace Nokogiri and other Ruby XML parsers for XML parsing and Marshal for object serialization.
Here is an example of using Ox:
require 'ox'
parsed_doc = Ox.parse(%{
<?xml?>
<Person>
<Name>Mary Active</Name>
<Age>21</Age>
</Person>
})
parsed_data = parsed_doc.Person.Name.textputs parsed_data
=> “Mary Active”
Advantages
- It is fast and efficient because it uses native XML parsing capabilities.
- It handles both XML and HTML, making it useful for a variety of tasks.
- It has a simple and intuitive API interface.
- Ox has fairly good performance and good support for most standard XML parsing features.
Disadvantages
- It may not be as widespread and popular as Nokogiri, and you may have to search for fewer support and learning resources.
- It may not be suitable for complex parsing scenarios that require many additional features and extensions.
Oga
Oga is another open-source HTML and XML parser written in Ruby. It uses a small native extension (C for MRI/Rubinius; Java for JRuby) to achieve greater performance. It provides a simple API for parsing, modifying, and querying documents using XPath, and supports XML namespaces (registration, query, etc.).
Here is an example of using Oga:
require 'oga'
parsed_doc = Oga.parse_xml(%{
<?xml?
<Person>
<Name>Mary Active</Name>
<Age>21</Age>
</Person>
})
parsed_data = parsed_doc.at_xpath('Person/Name/text()')puts parsed_data
=> "Mary Active"
Advantages
- It is easy to install on various platforms as it does not require system libraries like libxml.
- The API allows safe parsing and querying of documents in a multi-threaded environment.
- It has a small memory footprint.
- It comes with good documentation to help learn and use the library.
Disadvantages
- It has very few recent releases and is not very actively maintained, so it rarely receives updates and improvements.

LibXML Ruby
LibXML Ruby is a Ruby wrapper for the GNOME libxml2 XML library. It is open source and provides an incredible number of features such as XML validation, XPath support, XSLT support, etc.
Here is an example of using LibXML Ruby:
require 'xml'
parsed_doc = XML::Parser.string(%{
<Person>
<Name>Mary Active</Name>
<Age>21</Age>
</Person>
})
parsed_data = parsed_doc.parse.find('//Name').first.contentputs parsed_data
=> "Mary Active"
Advantages
- Very fast because it is written in C.
- Actively maintained with updates and improvements, frequent releases.
- Has good documentation.
- Performs automatic memory management.
Disadvantages
- It relies on libxml2, which in turn relies on libm, libz, and libiconv.
- It is not the best choice for parsing HTML since it supports parsing HTML4 documents, while most modern browsers use HTML5, which behaves differently.
Selenium WebDriver
Selenium WebDriver is an open-source browser automation tool that offers Ruby language bindings. It is much more than an HTML parsing library - it supports multiple browsers, handling dynamic web elements, a wide range of location strategies, mouse and keyboard events, and much more. This makes it popular among QA engineers for test validation when testing web applications.
Here is an example of using Selenium WebDriver:
require 'selenium-webdriver'
require 'webdrivers/chromedriver'driver = Selenium::WebDriver.for :chrome
driver.get('https://www.selenium.dev/selenium/web/web-form.html')puts driver.title
=> "Web Form"
Advantages
- It is one of the most popular and widely used QA tools, so it has very active community support.
- It is supported by numerous tutorials, blog posts, videos, and books, so you won't lack resources to learn and use this tool.
- It has very good documentation with examples.
- It is actively maintained and updated.
- It supports eight traditional web element location strategies: class name, CSS selector, id, name, link text, partial link text, tag name, and XPath.
Disadvantages
- Since Selenium WebDriver is a general-purpose browser automation library and is primarily used for QA, using Selenium WebDriver for HTML parsing may be overkill.
- To implement its functions, browser drivers must be installed.
- It is the slowest library in this list since it needs to interact with the browser through the browser driver.
- Selenium WebDriver does not support an automatic waiting mechanism, which can lead to unstable script behavior.
Conclusion
Web scraping is very useful for automatically extracting needed structured data from HTML and XML documents, especially if you do not have access to a public API that provides that data. The Ruby developer community provides several libraries to facilitate parsing these HTML and XML documents. In this article, we reviewed six of them to give you the ability to choose the appropriate parser for your specific use case.
Each parsing library has a unique set of features that, depending on your priorities, may be useful. If you are looking for a full-featured HTML and XML parser, look at Nokogiri. It is one of the most popular options, and there are plenty of resources to help you if you get stuck. If you do not want to use Nokogiri for HTML parsing, you can consider Hpricot, which provides a similar level of accuracy. If speed is your top priority, try Ox. On the other hand, LibXML Ruby or Oga could be great choices if you are looking for a library with a small memory footprint. Finally, if you need to simulate browser behavior when parsing a document, then Selenium WebDriver is the best choice.


