Path: | README |
Last Update: | Wed Jul 09 02:42:39 Mountain Daylight Time 2008 |
The libxml gem provides Ruby language bindings for GNOME’s Libxml2 XML toolkit. It is free software, released under the MIT License.
libxml-ruby provides several advantages over REXML:
libxml-ruby requires Ruby 1.8.4 or higher. It is dependent on the following libraries to function properly:
If you are running Linux or Unix you’ll need a C compiler so the extension can be compiled when it is installed. If you are running Windows, then install the Windows specific RubyGem which includes an already built extension.
The easiest way to install ruby-prof is by using Ruby Gems. To install:
gem install libxml-ruby
If you are running Windows, make sure to install the Win32 RubyGem which includes an already built binary file. The binary is built against libxml2 version 2.6.32 and iconv version 1.12. Both of these are also included as pre-built binaries, and should be put either in the libxml/lib directory or on the Windows path.
The Windows binaries are biult with MingW. The gem also includes a Microsoft VC++ 2005 solution. If you wish to run a debug version of libxml-ruby on Windows, then it is highly recommended you use VC++.
Basic usage for reading and writing documents.
There are several ways to read xml documents.
require 'libxml' doc = XML::Document.file('output.xml') root = doc.root puts "Root element name: #{root.name}" elem3 = root.find('elem3').to_a.first puts "Elem3: #{elem3['attr']}" doc.find('//root_node/foo/bar').each do |node| puts "Node path: #{node.path} \t Contents: #{node.content}" end
And your terminal should look like:
Root element name: root_node Elem3: baz Node path: /root_node/foo/bar[1] Contents: 1 Node path: /root_node/foo/bar[2] Contents: 2 Node path: /root_node/foo/bar[3] Contents: 3 Node path: /root_node/foo/bar[4] Contents: 4 Node path: /root_node/foo/bar[5] Contents: 5 Node path: /root_node/foo/bar[6] Contents: 6 Node path: /root_node/foo/bar[7] Contents: 7 Node path: /root_node/foo/bar[8] Contents: 8 Node path: /root_node/foo/bar[9] Contents: 9 Node path: /root_node/foo/bar[10] Contents: 10
To write a simple document:
require 'libxml' doc = XML::Document.new() doc.root = XML::Node.new('root_node') root = doc.root root << elem1 = XML::Node.new('elem1') elem1['attr1'] = 'val1' elem1['attr2'] = 'val2' root << elem2 = XML::Node.new('elem2') elem2['attr1'] = 'val1' elem2['attr2'] = 'val2' root << elem3 = XML::Node.new('elem3') elem3 << elem4 = XML::Node.new('elem4') elem3 << elem5 = XML::Node.new('elem5') elem5 << elem6 = XML::Node.new('elem6') elem6 << 'Content for element 6' elem3['attr'] = 'baz' format = true doc.save('output.xml', format)
The file output.xml contains:
<?xml version="1.0"?> <root_node> <elem1 attr1="val1" attr2="val2"/> <elem2 attr1="val1" attr2="val2"/> <elem3 attr="baz"> <elem4/> <elem5> <elem6>Content for element 6</elem6> </elem5> </elem3> <foo> <bar>1</bar> <bar>2</bar> <bar>3</bar> <bar>4</bar> <bar>5</bar> <bar>6</bar> <bar>7</bar> <bar>8</bar> <bar>9</bar> <bar>10</bar> </foo> </root_node>
See LICENSE for license information.
For more information please refer to the documentation. If you have any questions, please send email to libxml-devel@rubyforge.org.