Module: XMLStreamin

Defined in:
lib/xmlstreamin.rb

Overview

:title: XMLStreamin Documentation XMLStreamin is a small module that provides a way of reading XML as a stream, while letting it be processed acccording to its tree structure much more handily than the usual ‘flat’ stream reader does.

Unlike such readers, which typically simply call unspecialized methods for each start-tag, end-tag, text segment, and so on, and leave it to the application to sort out the hierarchy, XMLStreamin uses a pre-built tree of #XMLSpec nodes to model the expected document structure.

Each node specifies the actual actions to be taken when an element that it represents is encountered. It can specify what processing needs to be done on the attributes of a start-tag, the handling of included text, and any clean-up actions when the end-tag is read. It also contains a table of the expected sub-elements and their XMLSpec nodes, thus reflecting the document tree.

Usage skeleton:

require "xmlstreamin"
include XMLStreamin # for convenience in naming

# Supply XMLSpec objects to model the document tree: 

#First for the leaf elements:
someleafspec = XMLSpec.new
# override methods as needed, e.g.:
def someleafspec.text(context,data)
	#...
end

#... then provide nodes for the elements that enclose these:
someotherspec = XMLSpec.new
someotherspec.specs!({'someleaftag'=>someleafspec,...})
# Override methods in these as well, if appropriate:
def someotherspec.start(context,name,attrs)
	#...
	return context
end

#...... more specs complete the tree, up to some 'toplevelspec'
# for the document's enclosing element.

# Finally one spec for the document itself that just has
# an entry for that top level tag:
specDocument = XMLSpec.new
specDocument.specs!({'document_top_tag'=>toplevelspec})

To run, provide a source stream, and invoke the REXML stream parser:
source = ...the source of the document stream 
REXML::Document.parse_stream(source,
		XMLStreamListener.new(specDocument))

Defined Under Namespace

Classes: XMLError, XMLSpec, XMLStreamListener

Class Method Summary collapse

Class Method Details

.start(context, name, attrs) ⇒ Object

Raises:



196
197
198
# File 'lib/xmlstreamin.rb', line 196

def $specXMLFail.start(context,name,attrs)
	raise XMLError.new("Failed Tag <#{name}...>")
end