Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   REXML: Raw tags in element text? (http://www.velocityreviews.com/forums/t846010-rexml-raw-tags-in-element-text.html)

Jari Williamsson 11-23-2007 09:26 PM

REXML: Raw tags in element text?
 
In REXML, is it possible to add raw tags to element text, without having
to add elements for those tags?
(I would like to add a few HTML tags like <b></b> to strings, before I
know what element nodes they will appear at.)


Best regards,

Jari Williamsson


Phrogz 11-24-2007 01:51 AM

Re: REXML: Raw tags in element text?
 
On Nov 23, 2:26 pm, Jari Williamsson
<jari.williams...@mailbox.swipnet.se> wrote:
> In REXML, is it possible to add raw tags to element text, without having
> to add elements for those tags?
> (I would like to add a few HTML tags like <b></b> to strings, before I
> know what element nodes they will appear at.)


Are you wanting to ad XML elements as children of another without
using the DOM to create them (like setting .innerHTML in a web
browser, causing it to parse your string and create elements as
necessary),

or are you wanting to set the contents of a text node to have
characters that look like XML tags, but have it automatically escape
them for you?

Jari Williamsson 11-24-2007 06:47 AM

Re: REXML: Raw tags in element text?
 
Phrogz wrote:
> On Nov 23, 2:26 pm, Jari Williamsson
> <jari.williams...@mailbox.swipnet.se> wrote:
>> In REXML, is it possible to add raw tags to element text, without having
>> to add elements for those tags?
>> (I would like to add a few HTML tags like <b></b> to strings, before I
>> know what element nodes they will appear at.)

>
> Are you wanting to ad XML elements as children of another without
> using the DOM to create them (like setting .innerHTML in a web
> browser, causing it to parse your string and create elements as
> necessary),


Yes, this is what I want.


Best regards,

Jari Williamsson


Robert Klemme 11-24-2007 08:51 AM

Re: REXML: Raw tags in element text?
 
2007/11/24, Jari Williamsson <jari.williamsson@mailbox.swipnet.se>:
> Phrogz wrote:
> > On Nov 23, 2:26 pm, Jari Williamsson
> > <jari.williams...@mailbox.swipnet.se> wrote:
> >> In REXML, is it possible to add raw tags to element text, without having
> >> to add elements for those tags?
> >> (I would like to add a few HTML tags like <b></b> to strings, before I
> >> know what element nodes they will appear at.)

> >
> > Are you wanting to ad XML elements as children of another without
> > using the DOM to create them (like setting .innerHTML in a web
> > browser, causing it to parse your string and create elements as
> > necessary),

>
> Yes, this is what I want.


I believe you can do it (i.e. add raw tags to text) BUT they will not
show up as tags when output. Which is completely logical once you
think about it. If you work with a DOM you have to add nodes as
nodes. Maybe you can just create a small sub tree with a <b> node and
your text as nested text and later put that <b> node into the tree.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end


Jari Williamsson 11-24-2007 03:29 PM

Re: REXML: Raw tags in element text?
 
Robert Klemme wrote:
> 2007/11/24, Jari Williamsson <jari.williamsson@mailbox.swipnet.se>:
>> Phrogz wrote:
>>> On Nov 23, 2:26 pm, Jari Williamsson
>>> <jari.williams...@mailbox.swipnet.se> wrote:
>>>> In REXML, is it possible to add raw tags to element text, without having
>>>> to add elements for those tags?
>>>> (I would like to add a few HTML tags like <b></b> to strings, before I
>>>> know what element nodes they will appear at.)
>>> Are you wanting to ad XML elements as children of another without
>>> using the DOM to create them (like setting .innerHTML in a web
>>> browser, causing it to parse your string and create elements as
>>> necessary),

>> Yes, this is what I want.

>
> I believe you can do it (i.e. add raw tags to text) BUT they will not
> show up as tags when output. Which is completely logical once you
> think about it. If you work with a DOM you have to add nodes as
> nodes. Maybe you can just create a small sub tree with a <b> node and
> your text as nested text and later put that <b> node into the tree.


Since nothing seems to exist I wrote my own 2-line method: It adds a raw
string, parses it and passes all nodes to the parent element. Here's the
code if anyone else would need it:

---
class REXML::Element
def add_raw_text(raw_text)
doc = REXML::Document.new("<fake>"+raw_text+"</fake>")
doc.root.children.each { |e| self << e }
end
end
---

Example:
myelement.add_raw_text("here's a <b>bold</b> test")


Best regards,

Jari Williamsson



All times are GMT. The time now is 06:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.