The REXML which is a ruby ‘xml
processor’ is useful to parse the XML documents. I have used this
processor in my applications.
The usage is as follows.
you need to include it within your Ruby controller:
require “rexml/document”
include REXML
This includes the REXML library and includes the REXML namespace.
Save the following xml in a file (neils.xml)
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<letter title=”My mail”>
< file name=”neils”>
<note>
<to>arthur</to>
<from>ricky</from>
<heading>Hi Hello</heading>
<body>Hello How r u!</body>
<note>
<note>
<to>neil</to>
<from>ricky</from>
<heading>Hi Hello</heading>
<body>Hello Neil!</body>
<note>
</file>
</letter>
Access the contents of the xml file like this in the controller
doc = Document.new File.new(”neils.xml”)
print doc
You should be able to see the file contents(XML)
Now to access the nodes, use it as following.
doc.elements.each(”letter/file/note/body”)
{ |element| puts element.text }
