[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: humanized Xml tree navigation

Tim Bray

7/31/2006 3:16:00 PM

On Jul 31, 2006, at 6:16 AM, chiaro scuro wrote:

> What I need to do is to be able to traverse an xml document as in:
>
> * people.each_person {...}
> * people.person[2].name
> * people.person.name #takes the first person
> * people.person.size

Consider

<vocab-item>
<first.form>foo</first.form>
<second.form>f=F6=F6</second.form>
</vocab-item>

That's legal XML. Ruby n00b question: can you work around problems =
=20
like "vocab-item" and "first.form" in Ruby? -Tim

4 Answers

Daniel Harple

7/31/2006 3:28:00 PM

0

On Jul 31, 2006, at 11:16 AM, Tim Bray wrote:

> Consider
>
> <vocab-item>
> <first.form>foo</first.form>
> <second.form>föö</second.form>
> </vocab-item>
>
> That's legal XML. Ruby n00b question: can you work around problems
> like "vocab-item" and "first.form" in Ruby?

class Foo
define_method("first.form") do
"hi from first.form"
end
end
Foo.new.send("first.form") # => "hi from first.form"

-- Daniel


Logan Capaldo

7/31/2006 5:44:00 PM

0


On Jul 31, 2006, at 11:16 AM, Tim Bray wrote:

> On Jul 31, 2006, at 6:16 AM, chiaro scuro wrote:
>
>> What I need to do is to be able to traverse an xml document as in:
>>
>> * people.each_person {...}
>> * people.person[2].name
>> * people.person.name #takes the first person
>> * people.person.size
>
> Consider
>
> <vocab-item>
> <first.form>foo</first.form>
> <second.form>föö</second.form>
> </vocab-item>
>
> That's legal XML. Ruby n00b question: can you work around problems
> like "vocab-item" and "first.form" in Ruby? -Tim
>

Workaround is the word <g>

class A
define_method("@+$%!") { puts "Snoopy talk!" }
end

a = A.new
a.send("@+$%!")



Christian Neukirchen

7/31/2006 7:55:00 PM

0

Tim Bray <tbray@textuality.com> writes:

> On Jul 31, 2006, at 6:16 AM, chiaro scuro wrote:
>
>> What I need to do is to be able to traverse an xml document as in:
>>
>> * people.each_person {...}
>> * people.person[2].name
>> * people.person.name #takes the first person
>> * people.person.size
>
> Consider
>
> <vocab-item>
> <first.form>foo</first.form>
> <second.form>föö</second.form>
> </vocab-item>
>
> That's legal XML. Ruby n00b question: can you work around problems
> like "vocab-item" and "first.form" in Ruby? -Tim

Just make the API more flexible:

document["vocab-item"].bla["second.form"]

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Bob Hutchison

8/1/2006 12:21:00 PM

0

Hi,

On Jul 31, 2006, at 11:16 AM, Tim Bray wrote:

> On Jul 31, 2006, at 6:16 AM, chiaro scuro wrote:
>
>> What I need to do is to be able to traverse an xml document as in:
>>
>> * people.each_person {...}
>> * people.person[2].name
>> * people.person.name #takes the first person
>> * people.person.size
>
> Consider
>
> <vocab-item>
> <first.form>foo</first.form>
> <second.form>föö</second.form>
> </vocab-item>
>
> That's legal XML. Ruby n00b question: can you work around problems
> like "vocab-item" and "first.form" in Ruby? -Tim


I feel *compelled* to mention xampl (<http://rubyforge.org...
xampl/>). Using the current version for ruby, the program looks like
this:

thing = open("xml/funnystuff.xml"){ | f | XamplObject.from_xml_string
f.read }
puts thing.pp_xml
puts thing.class.name
thing.first_form.first.content = 'hello there'
puts thing.pp_xml

The output like this:

<vocab-item>
<first.form>foo</first.form>
<second.form>föö</second.form></vocab-item>
XamplAdHoc::VocabItem

<vocab-item>
<first.form>hello there</first.form>
<second.form>föö</second.form></vocab-item>

The file xml/funnystuff.xml has Tim's example xml in it.

The XamplAdHoc stuff is there because there is no namespace
declarations in the xml and I didn't supply a default mapping to a
Ruby Module.


Xampl does a lot more than this (in particular it does a weak-
transactional 'transparent' persistence kind of thing). It has been
around in Java since the late 1990s (where it does 'real'
transactions', CommonLisp for a couple of years, and Ruby for 18
months. I've written several programs well over 500 kloc using the
Java version. Currently using xampl in place of ActiveRecord in a
Rails project I'm working on (but you need the version I'm preparing
to release for use with Rails, so please be patient).


Cheers,
Bob

>

----
Bob Hutchison -- blogs at <http://www.rec...
hutch/>
Recursive Design Inc. -- <http://www.rec...>
Raconteur -- <http://www.raconteur...
xampl for Ruby -- <http://rubyforge.org...xampl/>