Using XML with Prototype

1 view
Skip to first unread message

KilLrBuNieZ

unread,
Oct 3, 2007, 9:39:29 AM10/3/07
to Ruby on Rails: Spinoffs
I am a new user of the prototype framework and am having problems
getting examples that address the type of issues I am having.

I am writing a random phrase generator by am using the Ajax.request to
read a static XML file. I want to use the Ajax.request result to query
according to a couple of factors. An example of the XML file
structured as follows:

<phrases>
<mood id="happy">
<phrase id="30">This happy phrase should be shown on random
numbers between 1 and 30.</phrase>
<phrase id="60">This happy phrase should be shown on random
numbers between 31 and 60.</phrase>
<phrase id="80">This happy phrase should be shown on random
numbers between 61 and 80.</phrase>
<phrase id="100">This happy phrase should be shown on random
numbers between 81 and 100.</phrase>
</mood>
<mood id="sad">
<phrase id="30">This sad phrase should be shown on random
numbers between 1 and 30.</phrase>
<phrase id="60">This sad phrase should be shown on random
numbers between 31 and 60.</phrase>
<phrase id="80">This sad phrase should be shown on random
numbers between 61 and 80.</phrase>
<phrase id="100">This sad phrase should be shown on random
numbers between 81 and 100.</phrase>
</mood>
</phrases>

On the HTML page I have a form with a select box to select the moods
(happy, sad, etc.) and a button to execute a Javascript function that
generates a random number and display the correct phrase.

My thinking is that I would create an Enumerable object that I would
load the XML into. Then I could use find() and/or other Enumerable
methods to locate the correct mood node and iterate to the phrase node
with the id that is less than or equal to the random number.

I can't seem to find enough documentation on prototype's Enumerable
object to construct my phrase object. Everything I've tried has not
worked so far. Anyone have any suggestions?

Thanks a million!

Thomas Fuchs

unread,
Oct 3, 2007, 1:44:30 PM10/3/07
to rubyonrail...@googlegroups.com

KilLrBuNieZ

unread,
Oct 4, 2007, 10:36:18 AM10/4/07
to Ruby on Rails: Spinoffs
I don't see how to convert my XML to JSON and use it. Any quick
examples you could give? If I got a random number 72, how would I find
the last one that has an id equal to or less than the number 72?
Reply all
Reply to author
Forward
0 new messages