jexml
Description
JEXML is a JRuby gem that provides a thin Ruby wrapper around the XML APIs in the JDK.
Features
-
Most compelling feature is the JDK support for XML document validation.
-
Full XPath support.
-
Supports parsing and updating XML documents.
-
Currently only DOM support; SAX support not yet implemented.
-
3 to 5 times better performance than REXML under JRuby.
-
Uses JDOM to properly format a document, due to bug in JDK.
The implementation approach is to keep the Ruby classes as thin as possible. Currently, it represents what was required for a specific project; the interface will get fleshed out to more completely cover JDK functionality in later releases.
Check out Nick Sieger’s JREXML gem at github.com/nicksieger/jrexml/ for a different approach for improving REXML performance under JRuby. This is a better way to go if you want to maintain REXML compatibility and do not need XML document validation.
Synopsis
Create a new JEXML::Document object, either from a File:
file = File.new("mydoc.xml")
doc = JEXML::Document.new file
or a String:
str = File.new("mydoc.xml").read
doc = JEXML::Document.new str
Use the methods on Document and XPath expressions to access nodes within the document:
doc.nodes("//inventory/section").each do |node|
puts "Found section #{node.name}..."
end
To validate the XML (i.e. against the DTD):
str = File.new("mydoc.xml").read
doc = JEXML::Document.validate(str)
To generate formatted XML as a String for a document:
doc.format
which only accepts a boolean parameter to indicate if the XML declaration should be omitted.
There are also methods for updating a document and nodes within a document. Check out the rdocs.
Requirements
JRuby / Java. Tested on JRuby 1.4.0 using Java 1.6.0_17 under Mac OS/X.
This product includes software developed by the JDOM Project (www.jdom.org/). The JDOM 1.1.1 library is included as part of the gem.
Includes software developed by the ActiveSupport Project (as.rubyonrails.org/). MIT license. The core_ext/blank.rb is included as part of the gem (unmodified from ActiveSupport 2.2.2).
Install
Install the gem under JRuby:
jruby -S gem install jexml
And require ‘jexml’.
gem 'jexml'
require 'jexml'
Source
You can get the JEXML source using Git:
git clone git://github.com/gboersma/jexml.git
License
(The MIT License)
Copyright © 2010 Gerald Boersma ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.