XML and HTML

REXML

rexml is a pure-Ruby XML parser that ships as a bundled gem: a full install has it, and a minimal one runs gem install rexml first. It loads a document into an element tree, and root.text reads the text inside the root:

ruby -e 'require "rexml/document"; d = REXML::Document.new("<a>x</a>"); puts d.root.text'

x

A document with a declaration, comments and namespaces parses the same way. REXML is slower than the C parsers and memory-hungry on a large file; for a config or an API response of a few kilobytes it is the one with no install step.

Attributes and XPath

attributes reads an attribute and elements takes an XPath expression. The two cover most of what a small feed needs:

ruby -e 'require "rexml/document"; d = REXML::Document.new(%q{<user id="7"><name>Ada</name></user>}); p d.root.attributes["id"]; p d.elements["//name"].text'

"7" "Ada"

elements.each("//item") walks a list, and each_element_with_attribute filters by an attribute. An XPath that starts with a single slash is absolute; one that starts with .// searches below the current node, which is what a loop over many items wants.

HTML is not XML

REXML requires well-formed XML: every tag closed, every attribute quoted. A page from the web rarely is, and the parser says so:

ruby -e 'require "rexml/document"; REXML::Document.new("<p>a<br></p>")'

.../rexml/parsers/baseparser.rb:456:in 'REXML::Parsers::BaseParser#pull_event': Missing end tag for 'br' (got 'p') (REXML::ParseException)

The line number follows the message. For HTML the answer is not to loosen the XML parser but to use one written for HTML, which knows the void elements and the optional closing tags.

When a parser gem is needed

nokogiri wraps libxml2 and a forgiving HTML parser, and it speaks CSS selectors as well as XPath. It builds a native extension, so these commands ran on the host in the ruby:3.4 image:

gem install nokogiri

1 gem installed

ruby -rnokogiri -e 'doc = Nokogiri::HTML("<p>a<br></p>"); p doc.at_css("p").text'

"a"

The parse repaired the unclosed br and the selector still found the text. The same library parses XML with Nokogiri::XML, and doc.errors lists what it had to repair — the detail that makes it usable on real pages.

A project that only reads a small, well-formed feed does not need the dependency. A scraper does, and the Gemfile says so.

What to watch for

An XML parser that expands entities is a security problem: a small document can define an entity that expands into gigabytes. REXML has limits, and Nokogiri::XML takes NONET to refuse network entities. Parse documents from strangers with the restrictive options.

text returns the text of one node; doc.text concatenates the whole tree, including whitespace between tags. Normalise the whitespace before comparing two documents.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does REXML::Document.new build from a document?

    Answer

    An element tree that can be read from the root down

    REXML parses the text into a tree of elements, attributes and text nodes.

  2. What does this program print? require "rexml/document" d = REXML::Document.new("<a>x</a>") puts d.root.text

    Answer

    x

    root is the a element, and text is the character data inside it.

  3. Which XPath expression searches below the current node rather than from the document root?

    Answer

    .//item

    A leading dot starts the search at the context node, which is what a loop over items needs.

  4. What does this program print? require "rexml/document" d = REXML::Document.new(%q{<user id="7"><name>Ada</name></user>}) p d.root.attributes["id"]

    Answer

    "7"

    An attribute value is a string, and p shows it with quotes.

  5. What does this program print? require "rexml/document" d = REXML::Document.new(%q{<user id="7"><name>Ada</name></user>}) p d.elements["//name"].text

    Answer

    "Ada"

    The XPath finds the name element and text returns its string value, which p quotes.

  6. What does this program print? require "rexml/document" begin REXML::Document.new("<p>a<br></p>") rescue => e puts e.class end

    Answer

    REXML::ParseException

    The unclosed br tag makes the document invalid XML, and REXML raises a parse exception that names the missing end tag.

  7. Why does a real web page fail in REXML?

    Answer

    It is HTML, where void elements and optional closing tags are legal and XML does not allow them

    HTML is not a well-formed XML document, so a parser written for XML rejects it.

  8. What does nokogiri add over REXML for a scraper?

    Answer

    A forgiving HTML parser plus XPath and CSS selectors

    Nokogiri repairs broken HTML and can select nodes by CSS as well as by XPath.

  9. Why is an XML parser that expands arbitrary entities a security problem?

    Answer

    A small document can define an entity that expands into gigabytes

    That is the entity expansion attack, and the restrictive options or a limit are the answer.

  10. What is the difference between node.text and doc.text in REXML?

    Answer

    node.text reads one node, while doc.text joins the whole tree including the whitespace between tags

    The whole-tree form collects the whitespace too, so it is normalised before two documents are compared.

Article author: Arthur Isaev