Creating mixed content elements in REXML

F

Francis Hwang

Say I have a string that represents an XML element that I want to
embed into a REXML::Element, how would I do this?

For example, if I have:

xhtml_elt = REXML::Element.new( 'xhtml' )
xhtml_string = "<p>foo <strong>bar</strong></p>"

Is there a way I could get to:

puts xhtml_elt.to_s
=> <xhtml><p>foo <strong>bar</strong></p></xhtml>

Francis
 
Z

Zachary P. Landau

--oyUTqETQ0mS9luUI
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Say I have a string that represents an XML element that I want to
embed into a REXML::Element, how would I do this?
=20
For example, if I have:
=20
xhtml_elt =3D REXML::Element.new( 'xhtml' )
xhtml_string =3D "<p>foo <strong>bar</strong></p>"
=20
Is there a way I could get to:
=20
puts xhtml_elt.to_s
=3D> <xhtml><p>foo <strong>bar</strong></p></xhtml>

I'm no expert, but after looking at the REXML source, I don't think this
is possible without changing the REXML code itself. I tried:"

xhtml_elt =3D REXML::Element.new('xhtml', nil, {:raw=3D>:all})
xhtml_elt.text =3D '<p>foo <strong>bar</strong></p>"

but the second line raises an Illegal character '<' in raw string blah
blah blah. There don't seem to be any options to avoid this. You
probably don't want to do this, but you could have the string parsed and
the correct REXML elements can be created. Sorry I couldn't help more.

--
Zachary P. Landau <[email protected]>
GPG: gpg --recv-key 0x24E5AD99 | http://kapheine.hypa.net/kapheine.asc

--oyUTqETQ0mS9luUI
Content-Type: application/pgp-signature
Content-Disposition: inline

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

iD8DBQFAIcq7CwWyMCTlrZkRAnsHAJ46vyb6cpCg/BijdtwWz9TB+3CULQCfX9lp
DKUtVzwsM1wRtq81dxNgRwA=
=8P37
-----END PGP SIGNATURE-----

--oyUTqETQ0mS9luUI--
 
F

Francis Hwang

Say I have a string that represents an XML element that I want to
embed into a REXML::Element, how would I do this?

For example, if I have:

xhtml_elt = REXML::Element.new( 'xhtml' )
xhtml_string = "<p>foo <strong>bar</strong></p>"

Is there a way I could get to:

puts xhtml_elt.to_s
=> <xhtml><p>foo <strong>bar</strong></p></xhtml>

Francis

Oh, for Pete's sake. All I had to do was use REXML::Document#root.

irb(main):001:0> require 'rexml/document'
=> true
irb(main):002:0> string = "<p>foo <strong>bar</strong></p>"
=> "<p>foo <strong>bar</strong></p>"
irb(main):003:0> REXML::Document.new( string ).root.to_s
=> "<p>foo <strong>bar</strong></p>"
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top