- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
At this point, we have the basics of the application, so let's take things a step farther. Aside from storing data, the most common thing you'll do with an array is loop through it looking for something. Ruby provides a number of different iterators, which you can use for any structure that involves multiple items, such as an array, list, and so on.
Will start with the each
iterator.
The each
Iterator
Now it would, of course, be useful for us to be able to iterate through a list of objects. As this is a topic that comes up most frequently with arrays, the let's look to arrays to solve the problem.
Arrays have available to them the each
iterator, which literally
enables you to iterate through each item in a list. Let's take a look
at how it works:
... puts "The second question is: " puts survey.questions[1].questionText puts "The third answer of the second question is: " puts survey.questions[1].answers[2].answerText sequelAnswers.each do |thisAnswer| puts thisAnswer.answerText end
Let's take this apart bit by bit. We start with the array, from which we
call the iterator. For each run through the iterator, we want to do something.
For that, we are creating a block between the do
keyword and the
end
keyword. Into the block, we are passing the current item for
the iterator. We are passing it as thisAnswer
, as indicated by the
pipe symbols (|).
Each time we run through the block, we can output the answerText
attribute of the thisAnswer
object. If we execute the script,
it looks like this:
Survey is called Retitled Survey The second question is: What is your favorite movie series? The third answer of the second question is: Halloween Star Wars Lord of the Rings Halloween None of the above
As it happens, we can also nest these blocks:
... puts "The second question is: " + survey.questions[1].questionText puts "The third answer of the second question is: " + survey.questions[1].answers[2].answerText survey.questions.each do |thisQuestion| puts thisQuestion.questionText thisQuestion.answers.each do |thisAnswer| puts " " + thisAnswer.answerText end end
In this case, we iterate through each of the questions that have been added to the survey, and for each one we output the text, and then iterate through the answers. The result gives us a pretty complete look at the survey:
Survey is called Retitled Survey The second question is: What is your favorite movie series? The third answer of the second question is: Halloween Do you like movie sequels? yes no What is your favorite movie series? Star Wars Lord of the Rings Halloween None of the above
In fact, this would be useful as the string version for the survey itself:
class Survey attr_reader :title, :questions attr_writer :title def initialize (givenTitle) @title = givenTitle @questions = Array.new end def to_s stringToReturn = "Survey: " + @title @questions.each do |thisQuestion| stringToReturn = stringToReturn + "\n" + thisQuestion.questionText thisQuestion.answers.each do |thisAnswer| stringToReturn = stringToReturn + "\n " + thisAnswer.answerText end end return stringToReturn end def addQuestion(question) @questions.push(question) end end ... puts "The second question is: " + survey.questions[1].questionText puts "The third answer of the second question is: " + survey.questions[1].answers[2].answerText puts survey
which gives us:
Survey is called Retitled Survey The second question is: What is your favorite movie series? The third answer of the second question is: Halloween Survey: Retitled Survey Do you like movie sequels? yes no What is your favorite movie series? Star Wars Lord of the Rings Halloween None of the above
Persisting Objects
Now, I actually had an ulterior motive in creating such a complete string version of the survey, and that's the fact that we are now going to look at saving the object to a file and getting it back out again.
As it happens, saving an object to a file is actually quite straightforward:
... puts "The second question is: " + survey.questions[1].questionText puts "The third answer of the second question is: " + survey.questions[1].answers[2].answerText puts "\n" puts survey File.open("savedSurvey", "w+") do |f| Marshal.dump(survey, f) end newlyLoadedSurvey = nil File.open("savedSurvey") do |f| newlyLoadedSurvey = Marshal.load(f) end puts "\nThe new survey is: \n\n" puts newlyLoadedSurvey
The first step is to get a writable reference to the new file in which we are
going to store the object. Notice that as before, the object that is returned by
the method, in this case open
, is passed to the reference in
the pipe symbols, in this case f
.
From there, we can use the Marshal
class's class method (think static),
dump
, to save a representation of the object to the file. To get it out again,
we create a new object, reopen the file, and load the file's contents into it.
Notice that we have not set the type for the newlyLoadedSurvey
object; all of the information comes from the persisted data. We can then
print the string representation of the object to the command line, as before:
Survey is called Retitled Survey The second question is: What is your favorite movie series? The third answer of the second question is: Halloween Survey: Retitled Survey Do you like movie sequels? yes no What is your favorite movie series? Star Wars Lord of the Rings Halloween None of the above The new survey is: Survey: Retitled Survey Do you like movie sequels? yes no What is your favorite movie series? Star Wars Lord of the Rings Halloween None of the above
Taking Stock
Okay, let's recap. We currently have a Ruby application with three classes, two of which exist as arrays stored and other objects. We looked at basic tasks such as getting information from the command line and outputting it back to the command line, iterating through arrays, and persisting objects in files.
Next, we'll look at putting this Ruby application on Rails.