class

XML::Reader

Inherits Reference / Object

XML::Reader is a parser for XML that iterates a XML document.

require "xml"

reader = XML::Reader.new(<<-XML)
  <message>Hello XML!</message>
  XML
reader.read
reader.name # => "message"
reader.read
reader.value # => "Hello XML!"

This is an alternative approach to XML.parse which parses an entire document into an XML data structure. XML::Reader offers more control and does not need to store the XML document in memory entirely. The latter is especially useful for large documents with the IO-based constructor.

WARNING: This type is not concurrency-safe.

Constructors

new(str : String, options : XML::ParserOptions = XML::ParserOptions.default)

Creates a new reader from a string.

See XML::ParserOptions.default for default options.

Source
new(io : IO, options : XML::ParserOptions = XML::ParserOptions.default)

Creates a new reader from an IO.

See XML::ParserOptions.default for default options.

Source

Instance methods

[](attribute : String) : String

Gets the attribute content for the attribute given by name. Raises KeyError if attribute is not found.

Source
[]?(attribute : String) : String | Nil

Gets the attribute content for the attribute given by name. Returns nil if attribute is not found.

Source
attributes_count

Returns attribute count of the node.

Source
depth

Returns the current nesting depth of the reader.

Source
document
Source
empty_element?

Checks if the node is an empty element.

Source
errors

Returns the errors reported while parsing.

Source
expand

Expands the node to a XML::Node that can be searched with XPath etc. The returned XML::Node is only valid until the next call to #read.

Raises a XML::Error if the node could not be expanded.

Source
expand?

Expands the node to a XML::Node that can be searched with XPath etc. The returned XML::Node is only valid until the next call to #read.

Returns nil if the node could not be expanded.

Source
has_attributes?

Checks if the node has any attributes.

Source
move_to_attribute(name : String) : Bool

Moves to the XML::Reader::Type::ATTRIBUTE with the specified name.

Source
move_to_element

Moves from the XML::Reader::Type::ATTRIBUTE to its containing XML::Reader::Type::ELEMENT.

Source
move_to_first_attribute

Moves to the first XML::Reader::Type::ATTRIBUTE of the node.

Source
move_to_next_attribute

Moves to the next XML::Reader::Type::ATTRIBUTE of the node.

Source
name

Returns the name of the node.

Source
next

Moves the reader to the next node while skipping subtrees.

Source
next_sibling

Moves the reader to the next sibling node while skipping subtrees.

Source
node_type

Returns the XML::Reader::Type of the node.

Source
read

Moves the reader to the next node.

Source
read_inner_xml

Returns the node's XML content including subtrees.

Source
read_outer_xml

Returns the XML for the node and its content including subtrees.

Source
value

Returns the text content of the node.

Source

Nested types