REXML : add text to element without temp variable?

C

CD Baby

Say I'm trying to make this XML:
<music>
<artist>Pink Floyd</artist>
<album>Dark Side of the Moon</album>
</music>

Right now I'm doing this:
doc = Document.new
music = doc.add_element 'music'
artist = music.add_element 'artist'
artist.text = 'Pink Floyd'
album = music.add_element 'album'
album.text = 'Dark Side of the Moon'

QUESTION:
Why create variables for artist and album?
Shouldn't there be a way to add the text directly, without creating an
in-between (and never used again) variable?

Like this:
music.add_element('album').text = 'Dark Side of the Moon'

or this:
music.add_element('album').text << Text.new('Dark Side of the Moon')

Has anyone found a shortcut like this?
 
A

Anders Engström

--zYM0uCDKw75PZbzx
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Say I'm trying to make this XML:
<music>
<artist>Pink Floyd</artist>
<album>Dark Side of the Moon</album>
</music>
=20
Right now I'm doing this:
doc =3D Document.new
music =3D doc.add_element 'music'
artist =3D music.add_element 'artist'
artist.text =3D 'Pink Floyd'
album =3D music.add_element 'album'
album.text =3D 'Dark Side of the Moon'
=20
QUESTION:
Why create variables for artist and album?
Shouldn't there be a way to add the text directly, without creating an
in-between (and never used again) variable?
=20
Like this:
music.add_element('album').text =3D 'Dark Side of the Moon'
=20
or this:
music.add_element('album').text << Text.new('Dark Side of the Moon')
=20
Has anyone found a shortcut like this?
=20

doc =3D Document.new
music =3D doc.add_element("music")
music << Element.new("artist").add_text("Pink Floyd")
music << Element.new("album").add_text("Dark side of the moon")

//Anders

--=20
=2E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
=2E Anders Engstr=F6m (e-mail address removed)
=2E http://www.gnejs.net PGP-Key: ED010E7F
=2E [Your mind is like an umbrella. It doesn't work unless you open it.] =
=20


--zYM0uCDKw75PZbzx
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA+kOeuNLLbe0BDn8RAioLAJoClSDPSvFw/Tde4nIDZscscBOeQwCcDSp0
EETjDe4R+sch+XD4UyCjMb4=
=e4H2
-----END PGP SIGNATURE-----

--zYM0uCDKw75PZbzx--
 
C

CD Baby

Thanks everyone for your help.
You're right, this works great:

doc2 = REXML::Document.new
music = doc2.add_element 'music'
music.add_element('artist').text = 'Pink Floyd'
music.add_element('album').text = 'Dark Side of the Moon'

Not sure why it wasn't working last night, except maybe I needed some
sleep. :)
 
G

Gavin Sinclair

Thanks everyone for your help.
You're right, this works great:
doc2 = REXML::Document.new
music = doc2.add_element 'music'
music.add_element('artist').text = 'Pink Floyd'
music.add_element('album').text = 'Dark Side of the Moon'
Not sure why it wasn't working last night, except maybe I needed some
sleep. :)

Or some more energetic music!? :)

Gavin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top