Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > REXML : add text to element without temp variable?

Reply
Thread Tools

REXML : add text to element without temp variable?

 
 
CD Baby
Guest
Posts: n/a
 
      07-18-2004
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?


 
Reply With Quote
 
 
 
 
Anders Engström
Guest
Posts: n/a
 
      07-18-2004
--zYM0uCDKw75PZbzx
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sun, Jul 18, 2004 at 12:09:25PM +0900, CD Baby wrote:
> 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 http://www.velocityreviews.com/forums/(E-Mail 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--


 
Reply With Quote
 
 
 
 
CD Baby
Guest
Posts: n/a
 
      07-18-2004
Thanks everyone for your help.
You're right, this works great:

doc2 = REXML:ocument.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.


 
Reply With Quote
 
Gavin Sinclair
Guest
Posts: n/a
 
      07-18-2004
On Monday, July 19, 2004, 1:32:55 AM, CD wrote:

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


> doc2 = REXML:ocument.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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
REXML::Element.write is deprecated. See REXML::Formatters Phlip Ruby 0 01-15-2008 08:23 PM
REXML: Raw tags in element text? Jari Williamsson Ruby 4 11-24-2007 03:29 PM
rexml error - REXML::Validation Daniel Berger Ruby 2 10-12-2004 04:19 PM
soap4r 1.4.8.1 with REXML 2.7.1 - no REXML::VERSION_MAJOR Damphyr Ruby 2 07-16-2003 09:49 AM



Advertisments