Module: SCXML

Defined in:
lib/scxml.rb,
lib/scxml/xpath.rb,
lib/scxml/element.rb,
lib/scxml/document.rb

Overview

SCXML

Fast, lightweight access to simple xml data.

Introduction

REXML is a standard, pure-Ruby XML processing library. It comes in the standard library and as far as I can tell is quite complete. It also is painfully slow when loading large XML documents and produces enormous memory footprints.

SCanningXML parses XML using a scanning approach that works very quickly and with a small memory footprint. SCXML can run in a lightweight mode that minimizes memory at the expense of access time.

Currently SCXML does not support insertion/removal of elements, although it may in the future.

SCXML does allow elements to be re-written with new attributes and content. For making XML documents, I generally use the Builder gem.

Elements can be accessed using XPath. Currently XPath support is incomplete, as well as the type of elements and content that can be parsed. See the limitations section below.

Copyright © 2007 Simon Chiang Version: 0.1 Licence: MIT-Style

Usage

require 'scxml'

 xml_string = %Q{
 <?xml version="1.0" encoding="UTF-8"?>
 <root>
  <elements>
    <element id="0">a</element>
    <element id="1">b</element>
    <element id="2">c</element>
  </elements>
 </root> }

# create a new xml document
doc = SCXML::Document.new(xml_string)

# select all elements
elements = doc.select('/root/elements/element')

# select element id = 1
e = doc.select('/root/elements/element[@id='1']')		
e.name					# -> 'element'
e.attribute('key') 			# -> '1'
e.atttributes				# -> {'id' => '1'}
e.content					# -> 'a'
e.rewrite(:attributes => {'id' => '10', 'new' => 'attr'}, :content => 'content')	# -> <element id='10' new='attr'>content</element>

# element creation sets the element to the first parsed node
r = SCXML::Element.new(xml_string)
r.name 					# -> 'root'

# elements select relative to themselves
e = r.select('elements')
e.name 					# -> 'elements'
elements = e.select('element')
elements.length				# -> 3
elements.first.attribute('key')		# -> '1'

Limitations

SCXML does not support all types of xml nodes. At the moment only simple xml documents will be correctly parsed by SCXML. The ‘Usage’ example is about SCXML can handle: elements with children or content, as well as attributes.

Supported XPath expressions are likewise limited to simple paths and predicates:

doc.select('/')				# -> selects the document
doc.select('/root')			# -> selects the root element
e = doc.select('/root/elements')	# -> selects the elements node
e.select('*')				# -> selects all children of the elements node
e.select('element')			# -> selects all children of the elements node named element
doc.select('//element')			# -> selects all element nodes wherever they occur
doc.select('//element[@id='1']')	# -> selects all element nodes with attribute id='1' wherever they occur

See the tests for specific expression support.

Defined Under Namespace

Modules: XPath Classes: Document, Element